轉: 深入理解 AngularJS 的 Scope

來源:互聯網
上載者:User

標籤:http   java   使用   io   ar   資料   div   cti   html   

 

查看 DEMO。參考 StackOverflow。

ng-switch

ng-switch 的原型繼承和 ng-include 一樣。所以如果你需要對基本類型資料進行雙向繫結,使用 $parent,或者將其改為 object 對象並綁定到對象的屬性,防止子 Scope 覆蓋父 Scope 的屬性。

參考 AngularJS, bind scope of a switch-case?

ng-repeat

ng-repeat 有一點不一樣。假設在我們的 controller 裡:

$scope.myArrayOfPrimitives = [ 11, 22 ];$scope.myArrayOfObjects    = [{num: 101}, {num: 202}]

還有 HTML:

<ul>    <li ng-repeat="num in myArrayOfPrimitives">       <input ng-model="num">    </li><ul><ul>    <li ng-repeat="obj in myArrayOfObjects">       <input ng-model="obj.num">    </li><ul>

對於每一個 Item,ng-repeat 建立新的 Scope,每一個 Scope 都繼承父 Scope,但同時 item 的值也被賦給了新 Scope 的新屬性(新屬性的名字為迴圈的變數名)。Angular ng-repeat 的源碼實際上是這樣的:

childScope = scope.$new(); // 子 scope 原型繼承父 scope ...     childScope[valueIdent] = value; // 建立新的 childScope 屬性

如果 item 是一個基礎資料類型(就像 myArrayOfPrimitives),本質上它的值被複製了一份賦給了新的子 scope 屬性。改變這個子 scope 屬性值(比如用 ng-model,即 num)不會改變父 scope 引用的 array。所以上面第一個 ng-repeat 裡每一個子 scope 獲得的 num 屬性獨立於 myArrayOfPrimitives 數組:

這樣的 ng-repeat 和你預想中的不一樣。在 Angular 1.0.2 及更早的版本,向文字框中輸入會改變灰色格子的值,它們只在子 Scope 中可見。Angular 1.0.3+ 以後,輸入文本不會再有任何作用了。(參考 StackOverflow 上的解釋)我們希望的是輸入能改變 myArrayOfPrimitives 數組,而不是子 Scope 裡的屬性。為此我們必須將 model 改為一個關於對象的數組(array of objects)。

所以如果 item 是一個對象,則對於原對象的一個引用(而非拷貝)被賦給了新的子 Scope 屬性。改變子 Scope 屬性的值(使用 ng-model,即 obj.num)也就改變了父 Scope 所引用的對象。所以上面第二個 ng-repeat 可表示為:

這才是我們想要的。輸入到文字框即會改變灰色格子的值,該值在父 Scope 和子 Scope 均可見。

參考 Difficulty with ng-model, ng-repeat, and inputs 以及 ng-repeat and databinding。

ng-controller

使用 ng-controller 進行嵌套,結果和 ng-include 和 ng-switch 一樣是正常的原型繼承。所以做法也一樣不再贅述。然而“兩個 controller 使用 $scope 繼承來共用資訊被認為是不好的做法”(來自 這裡),應該使用 service 在 controller 間共用資料。

如果你確實要通過繼承來共用資料,那麼也沒什麼特殊要做的,子 Scope 可以直接存取所有父 Scope 的屬性。參考 Controller load order differs when loading or navigating。

directives

這個要分情況來討論。

  1. 預設 scope: false – directive 不會建立新的 Scope,所以沒有原型繼承。這看上去很簡單,但也很危險,因為你會以為 directive 在 Scope 中建立了一個新的屬性,而實際上它只是用到了一個已存在的屬性。這對編寫可複用的模組和組件來說並不好。
  2. scope: true – 這時 directive 會建立一個新的子 scope 並繼承父 scope。如果在同一個 DOM 節點上有多個 directive 都要建立新 scope,則只有一個新 Scope 會建立。因為有正常的原型繼承,所以和 ng-include, ng-switch 一樣要注意基礎類型資料的雙向繫結,子 Scope 屬性會覆蓋父 Scope 同名屬性。
  3. scope: { ... } – 這時 directive 建立一個獨立的 scope,沒有原型繼承。這在編寫可複用的模組和組件時是比較好的選擇,因為 directive 不會不小心讀寫父 scope。然而,有時候這類 directives 又經常需要訪問父 scope 的屬性。對象散列(object hash)被用來建立這個獨立 Scope 與父 Scope 間的雙向繫結(使用 ‘=’)或單向綁定(使用 ‘@’)。還有一個 ‘&’ 用來綁定父 Scope 的運算式。這些統統從父 Scope 派生建立出本地的 Scope 屬性。注意,HTML 屬性被用來建立綁定,你無法在對象散列中引用父 Scope 的屬性名稱,你必須使用一個 HTML 屬性。例如,<div my-directive> 和 scope: { localProp: ‘@parentProp‘ } 是無法綁定父屬性 parentProp 到獨立 scope的,你必須這樣指定: <div my-directive the-Parent-Prop=parentProp> 以及scope: { localProp: ‘@theParentProp‘ }。獨立的 scope 中 __proto__ 引用了一個 Scope 對象(中的桔黃色 Object),獨立 scope 的 $parent 指向父 scope,所以儘管它是獨立的而且沒有從父 Scope 原型繼承,它仍然是一個子 scope。

    下面的圖中,我們有 <my-directive interpolated="{{parentProp1}}" twowayBinding="parentProp2"> 和 scope: { interpolatedProp: ‘@interpolated‘, twowayBindingProp: ‘=twowayBinding‘ }。
    同時,假設 directive 在它的 link 函數裡做了 scope.someIsolateProp = "I‘m isolated"



    注意:在 link 函數中使用 attrs.$observe(‘attr_name‘, function(value) { ... } 來擷取獨立 Scope 用 ‘@’ 符號替換的屬性值。例如,在 link 函數中有 attrs.$observe(‘interpolated‘, function(value) { ... } 值將被設為 11. (scope.interpolatedProp 在 link 函數中是 undefined,相反scope.twowayBindingProp 在 link 函數中定義了,因為用了 ‘=’ 符號)
    更多參考 http://onehungrymind.com/angularjs-sticky-notes-pt-2-isolated-scope/
  4. transclude: true – 這時 directive 建立了一個新的 “transcluded” 子 scope,同時繼承父 scope。所以如果模板片段中的內容(例如那些將要替代 ng-transclude 的內容)要求對父 Scope 的基本類型資料進行雙向繫結,使用 $parent,或者將 model 一個對象的屬性,防止子 Scope 屬性覆蓋父 Scope 屬性。

    transcluded 和獨立 scope (如果有)是兄弟關係,每個 Scope 的 $parent 指向同一個父 Scope。當模板中的 scope 和獨立 Scope 同時存在,獨立 Scope 屬性 $$nextSibling 將會指向模板中的 Scope。
    更多關於 transcluded scope 的資訊,參考 AngularJS two way binding not working in directive with transcluded scope。


    在中,假設 directive 和上個圖一樣,只是多了 transclude: true


    查看 線上 DEMO,例子裡有一個 showScope() 函數可以用來檢查獨立 Scope 和它關聯的 transcluded scope。
總結

一共有四種 Scope:

  1. 普通進行原型繼承的 Scope —— ng-include, ng-switch, ng-controller, directive with scope: true
  2. 普通原型繼承的 Scope 但拷貝賦值 —— ng-repeat。 每個 ng-repeat 的迴圈都建立新的子 Scope,並且子 Scope 總是獲得新的屬性。
  3. 獨立的 isolate scope —— directive with scope: {...}。它不是原型繼承,但 ‘=’, ‘@’ 和 ‘&’ 提供了訪問父 Scope 屬性的機制。
  4. transcluded scope —— directive with transclude: true。它也遵循原型繼承,但它同時是任何 isolate scope 的兄弟。

對於所有的 Scope,Angular 總是會通過 Scope 的 $parent, $$childHead 和 $$childTail 屬性記錄父-子關係。

轉: 深入理解 AngularJS 的 Scope

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.