【发布时间】:2014-01-23 07:31:54
【问题描述】:
我正在构建一个 AngularJS 指令。我希望该指令包装其中包含 ng-click 的其他内容。单击时生成的按钮不执行任何操作。 这是我尝试过的代码的简化版本:
(HTML)
<div ng-app="someapp">
<div ng-controller="Ctrl1">
<h2>Example</h2>
<my-dir data-x="1">
<button ng-click="refresh()" id="refresh1">Refresh</button>
</my-dir>
<my-dir data-x="2">
<button ng-click="refresh()" id="refresh2">Refresh</button>
</my-dir>
</div>
</div>
(JavaScript)
var app = angular.module('someapp', []);
app.controller('Ctrl1', function($scope){ });
app.directive('myDir', function(){
return {
restrict: 'E',
scope: {},
template: '<div><p>Directive contents:</p><div ng-transclude></div></div>',
transclude: true,
link: function(scope, element, attrs){
$scope.y = attrs.x+1;
scope.refresh = function(){
console.log("Refresh Called, y = ", $scope.y);
}
}
};
});
如何更改它以使按钮真正触发 $scope.refresh() 函数?
补充说明:
我需要指令的本地对象信息(单个控制器中可以有多个该指令),因此我创建了一个新范围。
【问题讨论】:
标签: javascript angularjs angularjs-directive