jsfiddle在这里。我一直在试验指令优先级和终端属性。我创建了三个优先级为 3、2 和 1 的指令。主指令(最高优先级,优先级:3)有一个创建按钮的模板,单击该按钮会调用指令控制器上的方法。一切正常,直到我将 terminal: true 放在优先级 2 指令上。由于某种原因导致按钮停止工作;主指令(优先级 3)呈现良好,但单击按钮什么也不做。同样,这里是jsfiddle,这里是指令的代码:
myApp = angular.module('myApp', [])
.directive('greeting', function() {
return {
restrict: 'E',
replace: true,
priority: 3,
template: "<button class='btn' ng-click='sayHello()'>Say Hello</button>",
controller: function($scope) {
var greetings = ['hello'];
$scope.sayHello = function() {
alert(greetings.join());
}
this.addGreeting = function(greeting) {
greetings.push(greeting);
}
}
};
})
.directive('finnish', function() {
return {
restrict: 'A',
priority: 2,
terminal:true,
require: 'greeting',
link: function(scope, element, attrs, controller) {
controller.addGreeting('hei');
}
};
})
.directive('hindi', function() {
return {
restrict: 'A',
priority: 1,
require: 'greeting',
link: function(scope, element, attrs, controller) {
controller.addGreeting('नमस्ते');
}
};
});
页面上的 html 如下所示:
<body ng-app="myApp">
<greeting finnish hindi />
</body>