【发布时间】:2013-12-18 10:53:55
【问题描述】:
我一直在尝试理解指令中孤立作用域和继承作用域之间的区别。这是我准备让自己理解的一个例子:
HTML
<div ng-controller="AppController">
<div my-directive>
Inside isolated scope directive: {{myProperty}}
</div>
<div my-inherit-scope-directive>
Inside inherited scope directive: {{myProperty}}
</div>
</div>
JS
angular.module("myApp", [])
.directive("myInheritScopeDirective", function() {
return {
restrict: "A",
scope: true
};
})
.directive("myDirective", function() {
return {
restrict: "A",
scope: {}
};
})
.controller("AppController", ["$scope", function($scope) {
$scope.myProperty = "Understanding inherited and isolated scope";
}]);
使用 Angular-1.1.5 执行代码,它按我的预期工作:由于隔离范围,my-directive 中的 {{myProperty}} 将是 undefined,而对于 my-inherit-scope-directive,{ {myProperty}} 的值为 Understanding inherited and isolated scope。
但是使用 Angular-1.2.1 执行,在两个指令 {{myProperty}} 中输出 Understanding inherited and isolated scope。
我缺少什么吗?
【问题讨论】:
标签: angularjs