我有以下模板的自定义指令。AngularJS我的自定义指令不能访问属性,但它似乎其他自定义指令可以
.directive('myDirective', ['$controller',function($controller) {
return {
templateUrl: 'client/test.ng.html',
scope: true,
controller: ['$scope','$attrs',function($scope,$attrs){
console.log($attrs)
}],
transclude: true,
}
}])
这个指令是被称为如下
<my-directive view="{{view}}"></my-directive>
<ion-tab ng-if="platform != 'android'" title="{{label}}" icon-off="{{off}}" icon-on="{{on}}" href="#/tab/{{view}}">
<my-directive view="{{view}}"></my-directive>
</ion-tab>
<ion-tab ng-if="platform == 'android'" title="{{label}}" class="tab-item" href="#/tab/{{view}}">
<my-directive view="{{view}}"></my-directive>
</ion-tab>
$attrs.view
是{{view}}
,uninterpolated。 <ion-tab>
将表达式内插到变量值中,显示正确的数据。
这对我来说很困惑。我已经在ion-tab
指令的内部和外部放置了my-directive
,以防出现某种范围问题。
访问表达式的值并使用该值反过来调用另一个指令的关键是什么?
背景:
我经历这一切的原因是因为
<ion-nav-view name="tab-{{view}}"></ion-nav-view>
不起作用。它,就像my-directive
似乎无法获得```view`的值,而是原始未解析的请求。我试图获取该值并直接调用该指令。
我似乎可以用$ scope获得我想要的值$ parent.view然后神秘地设置$ scope.view = $ scope。$ parent.view并设置子模板中的{{view}} DOESNT工作!?
更多的洞察力,执行console.log($ ATTRS)给出:
$…t.Attributes {$attr: Object, $$element: jQuery.fn.init[1], view: "{{view}}", class: "pane tab-content", navView: "active"}
然而,当这个被扩大,我们有
$$element: jQuery.fn.init[1]
$$observers: Object
$attr: Object
class: "pane tab-content"
navView: "active"
view: "dash"
__proto__: Object
任何帮助表示赞赏,感谢。
你可以请分享“查看”值获取的脚本吗?这个脚本看起来很好。 $ scope。$ parent.view是工作的,因为你在定义你的指令时设置了scope:true。因此,该指令原型继承了父范围 –
@RIYAJKHAN当然,这里是一个相关的问题更详细w/plunker链接:http://stackoverflow.com/questions/30854373/angularjs-cannot-interpolate-attribute-from-第一个指令到第二个w-pl – Babak
我已更新http://stackoverflow.com/questions/30854373/angularjs-cannot-interpolate-attribute-from-first-directive-to-a-second-w -pl/30859861#30859861。这里是最后一个小提琴.http://plnkr.co/edit/WoglpCyQmg9WYs5MrKYw?p = preview –