3 回答

TA貢獻1821條經驗 獲得超6個贊
要從隔離作用域指令內部在父作用域中調用控制器功能,請dash-separated在HTML中使用屬性名稱,如OP所述。
另外,如果要向函數發送參數,請通過傳遞對象來調用函數:
<test color1="color1" update-fn="updateFn(msg)"></test>
JS
var app = angular.module('dr', []);
app.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function(msg) {
alert(msg);
}
});
app.directive('test', function() {
return {
restrict: 'E',
scope: {
color1: '=',
updateFn: '&'
},
// object is passed while making the call
template: "<button ng-click='updateFn({msg : \"Hello World!\"})'>
Click</button>",
replace: true,
link: function(scope, elm, attrs) {
}
}
});

TA貢獻2039條經驗 獲得超8個贊
在“測試”指令Html標記中,函數的屬性名稱不應為駝峰式,而應基于破折號。
所以-代替:
<test color1="color1" updateFn="updateFn()"></test>
寫:
<test color1="color1" update-fn="updateFn()"></test>
這是angular區分指令屬性(例如update-fn函數)和函數之間差異的方法。
- 3 回答
- 0 關注
- 629 瀏覽
添加回答
舉報