對angularJs中controller控制器scope父子集作用域的實例講解
1.ctrl1是父級控制器,ctrl2和ctrl3都是ctrl1的子級控制器,
2.父級ctrl1中name值的改變會影響ctrl2和ctrl3中name值的改變,
3.但是ctrl2有自己的name輸入傳的值,不會影響ctrl1和ctrl3,這就是繼承隔離,
4.ctrl3無name賦值就繼承父級ctrl1中的name的值。
一、繼承隔離的情況
<div ng-app="module">
<div ng-controller="ctrl1">
{{name}}<input type="text" ng-model="name">
<div ng-controller="ctrl2">
{{name}}<input type="text" ng-model="name">
</div>
<div ng-controller="ctrl3">
{{name}}
</div>
</div>
</div>
<script>
var m = angular.module('module', []);
m.controller('ctrl1', ['$scope', function ($scope) {
$scope.name = '泠泠在路上'
}]);
m.controller('ctrl2', ['$scope', function ($scope) {
}]);
m.controller('ctrl3', ['$scope', function ($scope) {
}]);
</script>
運行結(jié)果:

二、繼承但不隔離
在ctrl2中改變name的值,既影響自己的值,也影響父級的值。
代碼:
<div ng-app="module">
<div ng-controller="ctrl1">
{{data.name}}<input type="text" ng-model="data.name">
<div ng-controller="ctrl2">
{{data.name}}<input type="text" ng-model="data.name">
</div>
<div ng-controller="ctrl3">
{{data.name}}
</div>
</div>
</div>
<script>
var m = angular.module('module', []);
m.controller('ctrl1', ['$scope', function ($scope) {
/* 定義對象*/
$scope.data={name:'泠泠在路上'}
}]);
m.controller('ctrl2', ['$scope', function ($scope) {
}]);
m.controller('ctrl3', ['$scope', function ($scope) {
}]);
</script>
運行結(jié)果:

以上這篇對angularJs中controller控制器scope父子集作用域的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
angular+ionic 的app上拉加載更新數(shù)據(jù)實現(xiàn)方法
這篇文章主要介紹了angular+ionic 的app上拉加載更新數(shù)據(jù)實現(xiàn)方法,需要的的朋友參考下2017-01-01
Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結(jié)
AngularJS 是一個 JavaScript 框架。它是一個以 JavaScript 編寫的庫。這篇文章主要介紹了Angular動態(tài)綁定樣式及改變UI框架樣式的方法小結(jié),需要的朋友可以參考下2018-09-09
Angular.Js中過濾器filter與自定義過濾器filter實例詳解
Angularjs過濾器是 angularjs非常棒的特性之一。有朝一日,你可能需要使用自定義過濾器,所以下面這篇文章主要給大家介紹了Angular.Js中過濾器filter與自定義過濾器filter的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Angular2中Bootstrap界面庫ng-bootstrap詳解
不知道大家有沒有留意,最近angular-ui團隊終于正式發(fā)布了基于 Angular2的Bootstrap界面庫ng-bootstrap ,之前工作中一直在用 AngularJS 1.x 的UI Bootstrap , 因此對這個ng-bootstrap 也是很感興趣,所以第一時間進行試用。這篇文章就給大家詳細介紹下ng-bootstrap。2016-10-10
angularjs中ng-bind-html的用法總結(jié)
這篇文章主要介紹了angularjs中ng-bind-html的用法總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

