我正在使用
“controller as” syntax,我将每个控制器放在一个包含在
IIFE中的单独文件中,如John Papa的
AngularJS Style Guide中所推荐的那样.
有没有办法从子控制器访问父控制器范围内声明的属性?
我可以通过执行{{parent.variableOfParent}}在视图中访问它们,但是不知道如何从子控制器的JS中执行此操作.
附:为样式指南创建标记可能会很好,因为有很多关于它的问题.
你本质上是在谈论在控制器之间共享数据.那么根据我[以及其他人也是如此;)],最好的方法是使用服务..
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>
原文地址:https://www.jb51.cc/angularjs/140382.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。