Copy <script src="//cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
<script type="text/javascript" src="app/index.js"></script>
<div ng-app="myApp">
<div ng-controller="firstController">
<input type="text" value="" ng-model="name"/>
</div>
<div ng-controller="secondController">
<input type="text" value="" ng-model="name"/>
</div>
</div>
Copy var app = angular .module ( 'myApp' , []);
app .controller ( 'firstController' , function ($scope){
$scope .name = '张三' ;
console .log ($scope);
});
app .controller ( 'secondController' , function ($scope){
console .log ($scope);
});
上面的代码均在myApp块中,其中有firstController和secondController两个控制器指令,而且是平级,所以secondController中的name不会受到影响。
当firstController包含secondController层级时,将会出现作用域的问题,secondController其实并未有name变量的赋值,这是则会向父级中找。
Copy <div ng-app="myApp">
<div ng-controller="firstController">
<input type="text" value="" ng-model="name"/>
<div ng-controller="secondController">
<input type="text" value="" ng-model="name"/>
</div>
</div>
</div>
<script src="//cdn.bootcss.com/angular.js/1.4.8/angular.js"></script>
<script type="text/javascript" src="app/index.js"></script>