我的情况非常简单。
<body>
<h1 nametag name="name" logsomthing="logsomthing()">Hello {{name}} <a href="#" ng-click="logsomthing()">Click here for alert</a></h1>
</body>
JS
angular.module('test', [])
.directive('nametag', function() {
return {
scope: {
name: '=',
logsomthing: '&'
},
controller: function($scope, $attrs, $log) {
$scope.name = 'John Doe';
$scope.logsomthing = function() {
alert('it is working');
}
},
link: function(scope, elem, attrs) {
}
};
});
它应该在点击时提醒(&#39;它正在工作&#39;)但没有任何反应。我无法找到解决方案。
答案 0 :(得分:0)
如果您希望它工作,您需要在指令中放置一个模板:
http://plnkr.co/edit/QXnDqDwQuyGhKpHZoCvy?p=preview
指令:
angular.module('test', [])
.directive('nametag', function() {
return {
scope: {
name: '@',
},
template:'Hello {{name}} <a href="#" ng-click="logsomthing()">Click here for alert</a>',
controller: function($scope, $attrs, $log) {
//$scope.name = 'John Doe'; // Not necessary anymore, sent by the view
$scope.logsomthing = function() {
alert('it is working');
}
},
link: function(scope, elem, attrs) {
}
};
});
查看:
<body>
<h1 nametag name="John Doe"></h1>
</body>
而且您也不需要定义3次功能logomthing