2012-08-17 67 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