0
AngularJS中可以嵌套控制器嗎?在嵌套控制器的情況下,$ scope對象是否在所有控制器之間共享。
的問題是: -
我可以訪問橫跨所有控制器的第一控制器$範圍值,但我不能讓第三控制器中的第二控制器範圍的對象。你可以在下面的代碼片段中運行並檢查它。
var app = angular.module("appTest",[]);
function Controller1($scope) {
$scope.text1= "text Sample 1"
}
function Controller2($scope) {
$scope.text2= "text sample 2"
}
function Controller3($scope) {
$scope.text3 = $scope.text1 + " And " + $scope.text2;
}
app.controller("Controller1",Controller1);
app.controller("Controller2",Controller2);
app.controller("Controller3",Controller3);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="appTest">
<div ng-controller="Controller1">
<div>
<h1> First Controller</h1>
First Controller Values is <b> {{text1}}</b>
</div>
<div ng-controller="Controller2">
<h1> Second Controller</h1>
First Controller Values is <b> {{text1}}</b>
<br />
Second controller value is <b> {{text2}}</b>
</div>
<div ng-controller="Controller3">
<h1> Thired Controller</h1>
First Controller Values + Second controller value is <b> {{text3}}</b>
</div>
</div>
</body>
https://github.com/angular/angular.js/wiki/Understanding-Scopes#user-content-ng-controller –