2012-08-17 62 views
33

这里是我的代码:我可以继承父控制器的变量吗?

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 
} 

我嵌套一个角controller另一个内。我想将第一个controller的变量传递给第二个。有谁知道如何做到这一点?

注意

我不能这样做

ChildCtrl.prototype = new ParentCtrl(); 

,因为我将覆盖ChildCtrlpeople财产。

+0

@ pkozlowski.opensource有什么想法? – dopatraman 2012-08-17 15:16:41

+0

也在子控制器中使用$ scope.people - 继承父级的属性。 – 2012-08-18 14:58:45

回答

46

默认情况下,子范围通过父范围原型继承(请参阅Scope),因此您已经可以访问父控制器的子范围属性。为了证明它:

function ChildCtrl($scope) { 
    alert($scope.people) 
} 
9

$ scope继承基于您使用ng-controller引用您的控制器的位置。

如果您有类似

​​

然后是,孩子控制器将继承父控制器的性能。

注意:孩子控制器不需要定义在HTML中的直接孩子。它可以是其中的任何小孩。

+2

我可以使用'Child's' HTML标记中的'Parent'属性,但不能使用'ChildController'的代码。您是否知道如何在“Child”功能中使用“Parent”的“人物”属性? – dopatraman 2012-08-17 15:36:16

+0

@codeninja如果你能给出更详细的要求,那将是一件好事。你想要达到什么目标,以及你在哪里/哪些地方遇到问题! – ganaraj 2012-08-17 15:54:30

+0

我想能够使用'ChildCtrl'中'ParentCtrl'的属性。我不知道如何再描述... – dopatraman 2012-08-17 16:02:17

25

你错了。您将控制器继承与范围继承混合在一起,并且它们在AngularJS中不同且松散耦合。

你真正想要的是:

function ParentCtrl($scope) { 
    $scope.people = ["Tom", "Dick", "Harry"]; 
    $scope.count = $scope.people.length; 
} 

function ChildCtrl($scope) { 
    $scope.parentpeople = $scope.$parent.people; 
} 

它将为案件工作:

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

但由于马克和ganaraj注意到上述情况,这没有任何意义,因为您可以访问来自 ParentCtrl和ChildCtrl的$ scope.people的属性。

如果你想相互继承控制器,你需要使用控制器函数本身的原型继承。

+1

没有简单的方法来继承AngularJS中的控制器。 – kstep 2012-10-04 14:34:44

+1

同意你的想法kstep。我只是和Codeninja一样困惑,所以我写了一篇文章,人们可能会觉得它有用http://www.michaeldausmann.com/2013/01/angularjs-scope-vs-controller.html – 2013-01-16 04:53:21

+0

@kstep谢谢非常 !我花了整整一天的努力来解决这个问题,并最终找到你的答案 – Anas 2013-08-05 12:03:40

4

您也可以通过DOM得到任何控制的范围:

$needleScope = angular.element(aDomElement).scope() 

使用jQuery:

$needleScope = $('#aDomElementId').scope() 

或者获取文档中的所有范围:

$allScopes = $('.ng-scope').scope() 
0

它可能帮你!!!

范围是一个特殊的JavaScript对象,它将控制器与视图连接起来。范围包含模型数据。在控制器中,模型数据通过$ scope对象访问。

<script> 
     var mainApp = angular.module("mainApp", []); 

     mainApp.controller("shapeController", function($scope) { 
     $scope.message = "In shape controller"; 
     $scope.type = "Shape"; 
     }); 
</script> 

作用域继承 作用域是控制器特定的。如果我们定义了嵌套控制器,那么子控制器将继承其父控制器的作用域。

<script> 
     var mainApp = angular.module("mainApp", []); 

     mainApp.controller("shapeController", function($scope) { 
     $scope.message = "In shape controller"; 
     $scope.type = "Shape"; 
     }); 

     mainApp.controller("circleController", function($scope) { 
     $scope.message = "In circle controller"; 
     }); 
</script> 

现场示例如下。

<html> 
    <head> 
     <title>Angular JS Forms</title> 
    </head> 
    <body> 
     <h2>AngularJS Sample Application</h2> 
     <div ng-app="mainApp" ng-controller="shapeController"> 
      <p>{{message}} <br/> {{type}} </p> 
      <div ng-controller="circleController"> 
      <p>{{message}} <br/> {{type}} </p> 
      </div> 
      <div ng-controller="squareController"> 
      <p>{{message}} <br/> {{type}} </p> 
      </div> 
     </div> 
     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> 
     <script> 
      var mainApp = angular.module("mainApp", []); 

      mainApp.controller("shapeController", function($scope) { 
      $scope.message = "In shape controller"; 
      $scope.type = "Shape"; 
      }); 

      mainApp.controller("circleController", function($scope) { 
      $scope.message = "In circle controller"; 
      }); 

      mainApp.controller("squareController", function($scope) { 
      $scope.message = "In square controller"; 
      $scope.type = "Square"; 
      }); 

     </script> 
    </body> 
    </html> 
+0

可能的原始来源:[AngularJS - 范围](http://www.hackersit.com/angularjs-scopes/)。 – 2015-10-08 09:23:10

+0

可能的原始来源:[为网络增强的Angular JS HTML](http://slides.com/arunjames/angular-js#/11)。 – 2015-10-08 09:24:08

+0

可能的原始来源:[了解AngularJS](http://www.tutorialspoint.com/angularjs/angularjs_tutorial.pdf)。 – 2015-10-08 09:24:57