我可以继承父控制器的变量? [英] can i inherit a parent controller's variables?
本文介绍了我可以继承父控制器的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code:
function ParentCtrl($scope) {
$scope.people = ["Tom", "Dick", "Harry"];
$scope.count = $scope.people.length;
}
function ChildCtrl($scope) {
$scope.parentpeople = ParentCtrl.people //this is what I would like to do ideally
}
我嵌套一个角控制器
另一个内部。我想第一个控制器
的变量传递给第二个。有谁知道如何做到这一点?
I am nesting one angular controller
inside of another one. I would like to pass variables of the first controller
to the second one. Does anyone know how to do this?
注意
我不能做这样的事情。
ChildCtrl.prototype = new ParentCtrl();
因为我将覆盖人
的属性 ChildCtrl
。
推荐答案
默认情况下,子作用域中典型从父继承的范围(见的范围),所以你已经有孩子访问父控制器的$作用域属性。为了证明这一点:
By default, child scopes prototypically inherit from the parent scope (see Scope), so you already have access to the parent controller's $scope properties in the child. To prove it:
function ChildCtrl($scope) {
alert($scope.people)
}
这篇关于我可以继承父控制器的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文