angularjs培訓之Scope嵌套

來源:互聯網
上載者:User

標籤:angularjs   scope   

    每部分的angularjs應用都有一個父scope(但是ng-app對應的是$rootScope,這個scope是終極scope),除了isolate scope,所有的scope都是通過原型繼承來建立,也就是說可以訪問它的父的scope.如果你對OOP比較熟悉,這種行為你看起來就比較熟悉了。

    每次html渲染屬性和方法是,會從當前的scope中尋找,如果沒有找到,就從他的父scope中尋找,直到檢索到$rootScope中,如果還沒有找到就會報錯。


下面看個例子:   

<DIV ng-controller="parentController">

parent:{{parent}} <br>

parent --> child : {{child}}

<div ng-controller="childController">

child : {{child}} <br>

child --> parent : {{parent}}

</div>

</DIV> 


controller中的定義如下:

var app = angular.module(‘demo‘,[]);

app.controller(‘parentController‘,  function($scope){

$scope.parent =‘parentString‘

});

app.controller(‘childController‘,  function($scope){

$scope.child = ‘childString‘

});


自己跑一下代碼啊,不要偷懶啊!

從運行結果上來看,子scope中可以直接存取父scope中的方法。如果在你的應用情境中,如果是公用的屬性就可以定義到父scope中。

好了,拜拜,下次見!

本文出自 “Eason's hcc” 部落格,請務必保留此出處http://hcc0926.blog.51cto.com/172833/1559742

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.