AngularJS 从子控制器访问父作用域 [英] AngularJS access parent scope from child controller

查看:26
本文介绍了AngularJS 从子控制器访问父作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 data-ng-controller="xyzController as vm"

设置了我的控制器

我有一个包含父/子嵌套控制器的场景.我使用 $parent.vm.property 访问嵌套 html 中的父属性没有问题,但我不知道如何从我的子控制器中访问父属性.

我尝试注入 $scope 然后使用 $scope.$parent.vm.property,但这不起作用?

有人可以提供建议吗?

解决方案

如果您的 HTML 如下所示,您可以这样做:

<div ng-controller="ChildCtrl">

然后就可以访问父作用域了

function ParentCtrl($scope) {$scope.cities = ["NY", "Amsterdam", "Barcelona"];}函数 ChildCtrl($scope) {$scope.parentcities = $scope.$parent.cities;}

如果您想从视图访问父控制器,您必须执行以下操作:

{{$parent.property}}

见jsFiddle:http://jsfiddle.net/2r728/

更新

实际上,由于您在父控制器中定义了 cities,您的子控制器将继承所有范围变量.所以理论上你不必调用 $parent.上面的例子也可以写成:

function ParentCtrl($scope) {$scope.cities = ["纽约","阿姆斯特丹","巴塞罗那"];}函数 ChildCtrl($scope) {$scope.parentCities = $scope.cities;}

AngularJS 文档使用这种方法,此处您可以阅读有关 $scope 的更多信息.

另一个更新

我认为这是对原始海报的更好回答.

HTML

<div ng-controller="ChildCtrl as cc"><pre>{{cc.parentCities |json}}</pre><pre>{{pc.cities |json}}</pre>

JS

function ParentCtrl() {var vm = 这个;vm.cities = [纽约"、阿姆斯特丹"、巴塞罗那"];}函数 ChildCtrl() {var vm = 这个;ParentCtrl.apply(vm, arguments);//继承父控件vm.parentCities = vm.cities;}

如果你使用 controller as 方法,你也可以按如下方式访问父作用域

function ChildCtrl($scope) {var vm = 这个;vm.parentCities = $scope.pc.cities;//注意 pc 是对ParentCtrl as pc"的引用}

如您所见,访问 $scopes 的方式有很多种.

更新小提琴

function ParentCtrl() {var vm = 这个;vm.cities = [纽约"、阿姆斯特丹"、巴塞罗那"];}函数 ChildCtrl($scope) {var vm = 这个;ParentCtrl.apply(vm, arguments);vm.parentCitiesByScope = $scope.pc.cities;vm.parentCities = vm.cities;}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script><div ng-app ng-controller="ParentCtrl as pc"><div ng-controller="ChildCtrl as cc"><pre>{{cc.parentCities |json}}</pre><pre>{{cc.parentCitiesByScope |json }}</pre><pre>{{pc.cities |json}}</pre>

I've set up my controllers using data-ng-controller="xyzController as vm"

I have a scenario with parent / child nested controllers. I have no problem accessing parent properties in the nested html by using $parent.vm.property, but I cannot figure out how to access the parent property from within my child controller.

I've tried injecting $scope and then using $scope.$parent.vm.property, but this isn't working?

Can anyone offer advice?

解决方案

If your HTML is like below you could do something like this:

<div ng-controller="ParentCtrl">
    <div ng-controller="ChildCtrl">
    </div>
</div>

Then you can access the parent scope as follows

function ParentCtrl($scope) {
    $scope.cities = ["NY", "Amsterdam", "Barcelona"];
}

function ChildCtrl($scope) {
    $scope.parentcities = $scope.$parent.cities;
}

If you want to access a parent controller from your view you have to do something like this:

<div ng-controller="xyzController as vm">
   {{$parent.property}}
</div>

See jsFiddle: http://jsfiddle.net/2r728/

Update

Actually since you defined cities in the parent controller your child controller will inherit all scope variables. So theoritically you don't have to call $parent. The above example can also be written as follows:

function ParentCtrl($scope) {
    $scope.cities = ["NY","Amsterdam","Barcelona"];
}

function ChildCtrl($scope) {
    $scope.parentCities = $scope.cities;
}

The AngularJS docs use this approach, here you can read more about the $scope.

Another update

I think this is a better answer to the original poster.

HTML

<div ng-app ng-controller="ParentCtrl as pc">
    <div ng-controller="ChildCtrl as cc">
        <pre>{{cc.parentCities | json}}</pre>
        <pre>{{pc.cities | json}}</pre>
    </div>
</div>

JS

function ParentCtrl() {
    var vm = this;
    vm.cities = ["NY", "Amsterdam", "Barcelona"];
}

function ChildCtrl() {
    var vm = this;
    ParentCtrl.apply(vm, arguments); // Inherit parent control

    vm.parentCities = vm.cities;
}

If you use the controller as method you can also access the parent scope as follows

function ChildCtrl($scope) {
    var vm = this;
    vm.parentCities = $scope.pc.cities; // note pc is a reference to the "ParentCtrl as pc"
}

As you can see there are many different ways in accessing $scopes.

Updated fiddle

function ParentCtrl() {
    var vm = this;
    vm.cities = ["NY", "Amsterdam", "Barcelona"];
}
    
function ChildCtrl($scope) {
    var vm = this;
    ParentCtrl.apply(vm, arguments);
    
    vm.parentCitiesByScope = $scope.pc.cities;
    vm.parentCities = vm.cities;
}
    

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app ng-controller="ParentCtrl as pc">
  <div ng-controller="ChildCtrl as cc">
    <pre>{{cc.parentCities | json}}</pre>
    <pre>{{cc.parentCitiesByScope | json }}</pre>
    <pre>{{pc.cities | json}}</pre>
  </div>
</div>

这篇关于AngularJS 从子控制器访问父作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆