【发布时间】:2013-12-10 05:02:32
【问题描述】:
html代码如下
<div class="dir-view" >
// unable to get {{ button.content }}
<button type="button" id="notworking" ng-show="button.content.length" >{{ button.content }}</button>
</div>
<div class="dir-design" >
//able to get {{ button.content }}
<button type="button" id="working" ng-show="button.content.length" >{{ button.content }}</button>
<div class="control-group">
<label class="label label-primary" for="button-content">BUTTON CONTENT :</label>
<div class="controls">
<textarea class="form-control" id="button-content" rows="4" cols="50" ng-model="button.content"></textarea>
</div>
</div>
</div>
在上面的代码中,dir-view 和 dir-design 是我定义的自定义指令。
在 div#dir-view 中的按钮标签中,我无法获取 {{ button.content }} 。
而在 div#dir-design 中,我可以获得 {{ button.content }}。
我认为 dir-design 指令中的范围对象在 dir-view 中是不可访问的。
这就是我无法在 dir-view 中访问 {{ button.content }} 的原因吗?
每个指令中的作用域是独立于该指令的吗?
如果我是对的,有人可以通过使 {{ button.content }} 的一个指令 dir-design 来指导我解决这个问题,即使在 dir-view 中也可以访问(即相邻指令)。
如果可能的话,请指导我阅读有关 Angular js 范围的好文档。
附加我从 angular debug chrome 插件获得的范围屏幕
我的controller.js文件内容显示指令
angular.module('modFoundation', ['$strap.directives'])
//directive to include view panel
.directive('dirView', function() {
return {
restrict : 'CAME',
templateUrl: 'templates/view.html',
transclude: true
};
})
//directive to include design panel
.directive('dirDesign', function() {
return {
restrict : 'CAME',
templateUrl: 'templates/design.html',
transclude: true
};
})
【问题讨论】:
-
您的问题包含很多内容,但尚不完全清楚您的目标是 IMO。如果您也可以发布一个 plunkr 或 jsfiddle 以显示问题的实际效果,这将有所帮助...从您的描述来看,您想在指令定义对象中使用 require 来访问另一个指令的控制器否则,您可能会受益于使用服务或值在指令之间共享一些数据。
-
我会想出一个 plunkr
-
是的会看那个教程..
-
这是一个网站,在相当短的部分中有很多很好的教程:egghead.io/search?q=directive
-
是的,正在调查。并将很快找到解决方案。感谢您的帮助。
标签: angularjs angularjs-directive angularjs-scope