【发布时间】:2013-11-18 03:57:56
【问题描述】:
我一直在研究 AngularJS,特别是看了视频:
http://www.thinkster.io/pick/IgQdYAAt9V/angularjs-directives-talking-to-controllers
本视频展示了一个指令与控制器对话的示例,我对其进行了一些修改,以尝试了解是否也可以使用隔离范围来获得类似的结果。考虑一个 HTML sn-p,例如:
<div enter="loadMoreTweets()">Roll Over This</div>
还有一个 Angular 控制器和指令定义为:
app.controller('scopeCtrl', function($scope) {
$scope.loadMoreTweets = function () {
alert("loading more tweets");
}
}).directive('enter', function() {
return {
restrict: "A",
scope: {enter: "@"},
link: function(scope, element, attrs) {
element.bind("mouseenter", function() {
//scope.$apply(attrs.enter);
scope.$apply(scope.enter);
})
}
}
});
在 DIV 上滚动不会导致错误并且没有任何效果。
如果我注释掉隔离范围并使用 element.bind() 中的注释行而不是对 scope.enter 的引用,则滚动 DIV 会导致 alert() 按预期显示。
问题:如果“@”隔离作用域在属性值和作用域属性之间创建了单向绑定,那么我会预料到scope.enter == attrs.enter。显然这不是真的。为什么?
【问题讨论】:
标签: angularjs-directive angularjs-scope angularjs-controller