我有一个angularJS示例
<div ng-controller="testCtrl">
<test color1="color1" updateFn="updateFn()"></test>
</div>
<script>
angular.module('dr', [])
.controller("testCtrl", function($scope) {
$scope.color1 = "color";
$scope.updateFn = function() {
alert('123');
}
})
.directive('test', function() {
return {
restrict: 'E',
scope: {color1: '=',
updateFn: '&'},
template: "<button ng-click='updateFn()'>Click</button>",
replace: true,
link: function(scope, elm, attrs) {
}
}
});
</script>
</body>
</html>
我想当我点击按钮时,警告框会出现,但什么也不会显示。
有谁可以帮我?
发布于 2016-06-29 04:15:07
使用破折号和小写作为属性名称(就像其他答案一样):
<test color1="color1" update-fn="updateFn()"></test>
在指令作用域中使用"=“而不是"&”:
scope: { updateFn: '='}
然后,您可以像使用任何其他函数一样使用updateFn:
<button ng-click='updateFn()'>Click</button>
就是这样!
发布于 2017-08-21 17:11:45
我不得不使用"=“绑定而不是"&”,因为这是不起作用的。奇怪的行为。
https://stackoverflow.com/questions/18378520
复制相似问题