通过指令被包裹,我怎么能访问它的范围是什么? [英] Being wrapped by a directive, how can I access its scope?
本文介绍了通过指令被包裹,我怎么能访问它的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以访问该指令的身体指令的适用范围隔离?我的DOM看起来是这样的:
How can I access the directive's isolate scope in the directive's body? My DOM looks like this:
<div ng-app="app">
<directive>
<p>boolProperty: {{boolProperty|json}}</p>
</directive>
</div>
的 boolProperty
指令的链接内部分配
功能:
angular.module("app", []).directive("directive", function() {
return {
scope: {},
link: function($scope) {
$scope.boolProperty = true;
}
};
});
问题是,孩子&LT; P&GT;
里面的指令绑定到该指令的父的范围,而不是指令的隔离范围。我该如何克服这个问题?
The problem is, the child <p>
inside the directive binds to the directive's parent scope, not the directive's isolated scope. How can I overcome this?
推荐答案
您忘了两件事情:
- 默认情况下AngularJS使用attrubute的限制,所以在指令定义你的情况,你应该指定
限制:E
- 您应该使用儿童范围,但不是孤立的。所以设置
范围:真正的
从父视图继承范围
- By default AngularJS uses attrubute restriction, so in your case in directive definition you should specify
restrict: "E"
- You should use child scope, but not isolated. So set
scope: true
to inherit from parent view scope.
请参阅更新小提琴 http://jsfiddle.net/Y9g4q/1/ 。
祝你好运。
这篇关于通过指令被包裹,我怎么能访问它的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文