angularjs – 当使用“controller As”和IIFE时,如何从子控制器
发布时间:2020-12-17 09:52:34 所属栏目:安全 来源:网络整理
导读:我正在使用 “controller as” syntax,我将每个控制器放在一个包含在 IIFE中的单独文件中,如John Papa的 AngularJS Style Guide中所推荐的那样. 有没有办法从子控制器访问父控制器范围内声明的属性? 我可以通过执行{{parent.variableOfParent}}在视图中访问
我正在使用
“controller as” syntax,我将每个控制器放在一个包含在
IIFE中的单独文件中,如John Papa的
AngularJS Style Guide中所推荐的那样.
有没有办法从子控制器访问父控制器范围内声明的属性? 我可以通过执行{{parent.variableOfParent}}在视图中访问它们,但是不知道如何从子控制器的JS中执行此操作. Plnkr 附:为样式指南创建标记可能会很好,因为有很多关于它的问题.
你本质上是在谈论在控制器之间共享数据.那么根据我[以及其他人也是如此;)],最好的方法是使用服务..
yourApp.service("sharedService",function() { this.data = "123"; }) yourApp.controller("parentController",function(sharedService) { $scope.sharedService = sharedService; }); yourApp.controller("childController",function(sharedService) { $scope.sharedService = sharedService; }); 现在,您希望在两个控制器之间“共享”的任何内容都可以放在服务中. 此外,在html中,您可以使用相同的服务引用来访问数据: 例如 <div ng-app='app' ng-controller='ParentCtrl as parent'> <div ng-controller='ChildCtrl as child'> {{parent.sharedService.data}}<br> <!-- both will print the same value --> {{child.sharedService.data}}<br> <!-- both will print the same value --> </div> </div> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |