JavaScript使用$ scope$ emit和$ scope$ on
首先,父子范圍關(guān)系確實(shí)很重要。你有兩種可能性發(fā)出某些事件:
$broadcast -將事件向下分發(fā)到所有子范圍,$emit-通過范圍層次結(jié)構(gòu)向上調(diào)度事件。我對你的控制器(作用域)關(guān)系一無所知,但是有幾種選擇:
如果scope of firstCtrl是作用域的父級(jí),則secondCtrl你的代碼應(yīng)通過替換$emit為$broadcastin來工作firstCtrl:
function firstCtrl($scope){ $scope.$broadcast(’someEvent’, [1,2,3]);}function secondCtrl($scope){ $scope.$on(’someEvent’, function(event, mass) { console.log(mass); });}
如果你的范圍之間沒有父子關(guān)系,則可以注入$rootScope控制器并將事件廣播到所有子范圍(即secondCtrl)。
function firstCtrl($rootScope){ $rootScope.$broadcast(’someEvent’, [1,2,3]);}最后,當(dāng)你需要將事件從子控制器分派到向上作用域時(shí),可以使用$scope.$emit。如果的范圍firstCtrl是范圍的父級(jí)secondCtrl:
function firstCtrl($scope){ $scope.$on(’someEvent’, function(event, data) { console.log(data); });}function secondCtrl($scope){ $scope.$emit(’someEvent’, [1,2,3]);}解決方法
如何$scope使用.$emit和.$on方法將對象從一個(gè)控制器發(fā)送到另一個(gè)控制器?
function firstCtrl($scope) { $scope.$emit(’someEvent’,[1,2,3]);}function secondCtrl($scope) { $scope.$on(’someEvent’,function(mass) { console.log(mass); });}
它不按我認(rèn)為的方式工作。如何做$emit和$on工作?
相關(guān)文章:
1. boot2docker無法啟動(dòng)2. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””3. docker-compose中volumes的問題4. javascript - mock.js可以存儲(chǔ)數(shù)據(jù)嗎5. nignx - docker內(nèi)nginx 80端口被占用6. java - SSH框架中寫分頁時(shí)service層中不能注入分頁類7. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.8. dockerfile - 為什么docker容器啟動(dòng)不了?9. golang - 用IDE看docker源碼時(shí)的小問題10. docker api 開發(fā)的端口怎么獲取?

網(wǎng)公網(wǎng)安備