AngularJS全局scope與Isolate scope通信用法示例
本文實(shí)例講述了AngularJS全局scope與Isolate scope通信用法。分享給大家供大家參考,具體如下:
在項(xiàng)目開發(fā)時(shí),全局scope 和 directive本地scope使用范圍不夠清晰,全局scope與directive本地scope通信掌握的不夠透徹,這里對(duì)全局scope 和 directive本地scope的使用做一個(gè)總結(jié)。
一、scope作用域
1、AngularJS中,子作用域一般都會(huì)通過(guò)JavaScript原型繼承機(jī)制繼承其父作用域的屬性和方法。但有一個(gè)例外:在directive中使用scope: { ... },這種方式創(chuàng)建的作用域是一個(gè)獨(dú)立的"Isolate"作用域,它也有父作用域,但父作用域不在其原型鏈上,不會(huì)對(duì)父作用域進(jìn)行原型繼承。這種方式定義作用域通常用于構(gòu)造可復(fù)用的directive組件.
2、如果我們?cè)谧幼饔糜蛑性L問(wèn)一個(gè)父作用域中定義的屬性,JavaScript首先在子作用域中尋找該屬性,沒(méi)找到再?gòu)脑玩溕系母缸饔糜蛑袑ふ?,如果還沒(méi)找到會(huì)再往上一級(jí)原型鏈的父作用域?qū)ふ摇T贏ngularJS中,作用域原型鏈的頂端是$rootScope,JavaScript尋找到$rootScope為止.
3、scope: { ... } - directive創(chuàng)建一個(gè)獨(dú)立的“Isolate”作用域,沒(méi)有原型繼承。這是創(chuàng)建可復(fù)用directive組件的最佳選擇。因?yàn)樗粫?huì)直接訪問(wèn)/修改父作用域的屬性,不會(huì)產(chǎn)生意外的副作用。
二、Isolate scope 引用修飾符
1、 = or =attr “Isolate”作用域的屬性與父作用域的屬性進(jìn)行雙向綁定,任何一方的修改均影響到對(duì)方,這是最常用的方式;
2、 @ or @attr “Isolate”作用域的屬性與父作用域的屬性進(jìn)行單向綁定,即“Isolate”作用域只能讀取父作用域的值,并且該值永遠(yuǎn)的String類型;
3、 & or &attr “Isolate”作用域把父作用域的屬性包裝成一個(gè)函數(shù),從而以函數(shù)的方式讀寫父作用域的屬性,包裝方法是$parse
三、directive 與 controller 數(shù)據(jù)傳遞和通信
1、父controller監(jiān)聽(tīng)全局scope(父scope)變量, 并廣播事件給子scope(directive scope,每個(gè)directvie都有自己獨(dú)立的scope作用域)
2、directive 定義本地scope,通過(guò)=、@、&(方法)字符顯示引用全局scope
3、directive scope(子scope)通過(guò)parent[$scope.$parent.xxx]引用全局scope的屬性
4、directive監(jiān)聽(tīng)全局scope變量變化,可以通過(guò)$scope.$parent.$watch方法
四、實(shí)例說(shuō)明
<div ng-controller="MyCtrl"> <button ng-click="show=true">show</button> <dialog title="Hello }" visible="}" on-cancel="show=false;" on-ok="show=false;parentScope();"> <!--上面的on-cancel、on-ok,是在directive的isoloate scope中通過(guò)&引用的。 如果表達(dá)式中包含函數(shù),那么需要將函數(shù)綁定在parent scope(當(dāng)前是MyCtrl的scope)中--> Body goes here: username:} , title:}. <ul> <!--這里還可以這么玩~names是parent scope的--> <li ng-repeat="name in names">}</li> </ul> <div> Email:<input type="text" ng-model="email" style="width: 200px;height:20px"/> </div> <div> Count:<input type="text" ng-model="person.Count" style="width: 120px;height:20px"/> <button ng-click="changeCount()">Count加1</button> </div> <p></p> </dialog> </div>
Controller 測(cè)試代碼:
var app = angular.module("Dialog", []); app.controller("MyCtrl", function ($scope) { $scope.person = { Count: 0 }; $scope.email = 'carl@126.com'; $scope.names = ["name1", "name2", "name3"]; $scope.show = false; $scope.username = "carl"; $scope.title = "parent title"; $scope.parentScope = function () { alert("scope里面通過(guò)&定義的東東,是在父scope中定義"); }; $scope.changeCount = function () { $scope.person.Count = $scope.person.Count + 1; } // 監(jiān)聽(tīng)controller count變更, 并發(fā)出事件廣播,再directive 中 監(jiān)聽(tīng)count CountStatusChange變更事件 $scope.$watch('person.Count', function (newVal, oldVal) { console.log('>>>parent Count change:' + $scope.person.Count); if (newVal != oldVal) { console.log('>>>parent $broadcast count change'); $scope.$broadcast('CountStatusChange', {"val": newVal}) } }); }); app.directive('dialog', function factory() { return { priority: 100, template: ['<div ng-show="visible">', ' <h3>}</h3>', ' <div class="body" ng-transclude></div>', ' <div class="footer">', ' <button ng-click="onOk()">OK</button>', ' <button ng-click="onCancel()">Close</button>', ' </div>', '</div>'].join(""), replace: false, transclude: true, restrict: 'E', scope: { title: "@",//引用dialog標(biāo)簽title屬性的值 visible: "@",//引用dialog標(biāo)簽visible屬性的值 onOk: "&",//以wrapper function形式引用dialog標(biāo)簽的on-ok屬性的內(nèi)容 onCancel: "&"http://以wrapper function形式引用dialog標(biāo)簽的on-cancel屬性的內(nèi)容 }, controller: ['$scope', '$attrs', function ($scope, $attrs) { // directive scope title 通過(guò)@ 引用dialog標(biāo)簽title屬性的值,所以這里能取到值 console.log('>>>title:' + $scope.title); >>>title:Hello carl scope.html:85 // 通過(guò)$parent直接獲取父scope變量頁(yè)可以 console.log('>>>parent username:' + $scope.$parent.username); >>>parent username:carl // directive scope 沒(méi)有定義username 變量,并且沒(méi)有引用父scope username變量, 所以這里是undefined console.log('>>>child username:' + $scope.username); >>>username:undefined // 接收由父controller廣播count變更事件 $scope.$on('CountStatusChange', function (event, args) { console.log("child scope on(監(jiān)聽(tīng)) recieve count Change event :" + args.val); }); // watch 父 controller scope對(duì)象 $scope.$parent.$watch('person.Count', function (newVal, oldVal) { console.log('>>>>>>>child watch parent scope[Count]:' + oldVal + ' newVal:' + newVal); }); }] }; });
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
- AngularJS控制器之間的通信方式詳解
- AngularJS控制器controller正確的通信的方法
- AngularJS控制器之間的數(shù)據(jù)共享及通信詳解
- AngularJS開發(fā)教程之控制器之間的通信方法分析
- AngularJS入門心得之directive和controller通信過(guò)程
- 詳解AngularJS的通信機(jī)制
- angular中不同的組件間傳值與通信的方法
- Angularjs的Controller間通信機(jī)制實(shí)例分析
- Angular2 父子組件通信方式的示例
- Angularjs實(shí)現(xiàn)控制器之間通信方式實(shí)例總結(jié)
相關(guān)文章
Angular應(yīng)用程序的Hydration基本概念詳解
這篇文章主要為大家介紹了Angular應(yīng)用程序的Hydration基本概念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09AngularJs的UI組件ui-Bootstrap之Tooltip和Popover
這篇文章主要介紹了AngularJs的UI組件ui-Bootstrap之Tooltip和Popover,tooltip和popover是輕量的、可擴(kuò)展的、用于提示的指令。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07angularjs實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了angularjs實(shí)現(xiàn)多張圖片上傳并預(yù)覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02AngularJS中的Directive自定義一個(gè)表格
本篇文章給大家介紹在angularjs中自定義一個(gè)有關(guān)表格的directive,涉及到angularjs directive相關(guān)知識(shí),對(duì)本文感興趣的朋友一起學(xué)習(xí)吧2016-01-01解決angular2 獲取到的數(shù)據(jù)無(wú)法實(shí)時(shí)更新的問(wèn)題
今天小編就為大家分享一篇解決angular2 獲取到的數(shù)據(jù)無(wú)法實(shí)時(shí)更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08使用Angular Cli如何創(chuàng)建Angular私有庫(kù)詳解
這篇文章主要給大家介紹了關(guān)于使用Angular Cli如何創(chuàng)建Angular私有庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01Angularjs中的驗(yàn)證input輸入框只能輸入數(shù)字和小數(shù)點(diǎn)的寫法(推薦)
這篇文章主要介紹了Angularjs中的驗(yàn)證input輸入框只能輸入數(shù)字和小數(shù)點(diǎn)的寫法,需要的朋友可以參考下2017-08-08AngularJS遞歸指令實(shí)現(xiàn)Tree View效果示例
這篇文章主要介紹了AngularJS遞歸指令實(shí)現(xiàn)Tree View效果,結(jié)合實(shí)例形式分析了AngularJS基于遞歸指令實(shí)現(xiàn)樹形結(jié)構(gòu)層次數(shù)據(jù)的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-11-11