当范围在指令上继承时,我可以让 ng-click 工作,但在隔离时则不行。UPDATE: The point is that I want the click function to be defined as part of the directive... moving the function definition into a different scope is not what I want.
这是具有继承范围的工作示例:https://codepen.io/anon/pen/PGBQvj https://codepen.io/anon/pen/PGBQvj
这是具有隔离范围的损坏示例;https://codepen.io/anon/pen/jrpkjp https://codepen.io/anon/pen/jrpkjp
(单击数字,它们在第一个示例中增加,但在第二个示例中不增加)
一些代码...
The HTML
<div ng-app="myApp" ng-controller="baseController">
<my-directive ng-click="hello()" current="current"></my-directive>
</div>
具有继承范围的指令:
angular.module('myApp', [])
.controller('baseController', function($scope) {
$scope.current = 1;
})
.directive('myDirective', function() {
return {
link: function(scope, element, attrs) {
scope.hello = function() {
scope.current++
};
},
replace: true,
scope: true,
template: '<div><child> <strong>{{ current }}</strong></child></div>'
}
})
.directive('child', function() {
return {
link: function(scope, element, attrs) {
console.log("horeee");
}
}
});
相同的指令但具有独立的范围:
angular.module('myApp', [])
.controller('baseController', function($scope) {
$scope.current = 1;
})
.directive('myDirective', function() {
return {
link: function(scope, element, attrs) {
scope.hello = function() {
scope.current++
};
},
replace: true,
scope: {
current:'='
},
template: '<div><child> <strong>{{ current }}</strong></child></div>'
}
})
.directive('child', function() {
return {
link: function(scope, element, attrs) {
console.log("horeee");
}
}
});