大佬教程收集整理的这篇文章主要介绍了angularjs – 在Transcluded指令中访问父级作用域,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的想避免在MyCtrl中加入类似SOME_CONST(这将有助于我通过控制流更新DOM)
<div ng-controller="MyCtrl"> <parent> <child></child> </parent> </div> var myApp = angular.module('myApp',[]); function MyCtrl($scopE) { $scope.obj = {prop:'foo'}; } myApp.directive('parent',function() { return { scope: true,transclude: true,reStrict: 'EA',template: '<div ng-transclude><h1>I\'m parent {{obj.prop}}<h1></div>',link: function(scope,elem,attrs) { scope@L_806_2@mE_CONST = 'someConst'; } } }); myApp.directive('child',function() { return { reStrict: 'EA',template: '<h1>I\'m child.... I want to access my parent\'s stuff,but I can\'t. I can access MyCtrlScope though,see <b>{{obj.prop}}</b></h1> how can I access the <b>SOME_CONST</b> value in my parent\'s link function? is this even a good idea? {{SOME_CONST}}. I really don\'t want to put everything inside the MyCtrl',} });
请看这fiddle
谢谢
范围004是范围:true的结果,范围005是transclude:true的结果。由于child指令不创建一个新的范围,它使用转录的范围005.从图中可以看出,没有从范围005到范围004的路径(除了通过私有属性$$ prevSibling,这是在相反的方向$$ nextSibling – 但不要使用那些。)
@ joakimbl的解决方案可能是最好的,虽然我认为更常见的是在父指令的控制器上定义一个API,而不是定义属性:
controller: function($scopE) { $scope@L_806_2@mE_CONST = 'someConst'; this.getConst = function() { return $scope@L_806_2@mE_CONST; } }
然后在child指令中:
link:function(scope,element,attrs,parentCtrl){ scope@L_806_2@mE_CONST = parentCtrl.getConst(); },
以上是大佬教程为你收集整理的angularjs – 在Transcluded指令中访问父级作用域全部内容,希望文章能够帮你解决angularjs – 在Transcluded指令中访问父级作用域所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。