詳解在AngularJS的controller外部直接獲取$scope
以前利用webqq的寫過一個(gè)自動(dòng)發(fā)消息的腳本,由于那時(shí)webqq還直接使用類似jQuery操作DOM的技術(shù),所以腳本很簡(jiǎn)單就可以實(shí)現(xiàn)。
現(xiàn)如今很多web應(yīng)用都開始使用AngularJS,MVVM導(dǎo)致的就是無法操作dom而去直接改變數(shù)據(jù)。所以面對(duì)網(wǎng)頁版微信,要實(shí)現(xiàn)一個(gè)自動(dòng)發(fā)送的腳本,就無法再用以前那套DOM的思路了。
要修改AngularJS中的數(shù)據(jù),首先就要獲取scope。其實(shí)獲取scope的方法很簡(jiǎn)單。
由于大部分Angular項(xiàng)目需要使用jQuery作為補(bǔ)充??梢栽趈Query中通過.scope()方法獲取當(dāng)前選擇器內(nèi)容里繼承的域。
即類似這樣的方式:
$('div[ng-controller="listController"]').scope();
例:完整的例子。
<!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta charset="UTF-8"> <title>Get angular's scope in jQuery</title> <script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.js"></script> <script src="http://cdn.bootcss.com/angular.js/1.4.3/angular.js"></script> <script> angular.module('app',[]) .controller('listController',['$scope', function ($scope) { $scope.list = [1,2,3,4,5]; $scope.test = function () { console.log('test'); } }]) </script> <script> $(document).on('ready', function () { var controllerScope = $('div[ng-controller="listController"]').scope(); // Get controller's scope controllerScope.test(); // log 'test' console.log(controllerScope.list); // log [1,2,3,4,5] $('button').click(function (e) { var scope = $(e.target).scope(); console.log(scope.item) // log item number scope.test(); // log 'test' }) }) </script> </head> <body> <div ng-controller="listController"> <ul> <li ng-repeat="item in list"><button>Select {{item}}</button></li> </ul> </div> </body> </html>
那么打開網(wǎng)頁版微信的頁面(2016-06-05)選中你需要發(fā)消息的人。然后執(zhí)行如下腳本:
var controllerScope = $('div[ng-controller="chatSenderController"]').scope(); // 獲取chatSenderController的$scope controllerScope.editAreaCtn = "星哥,下午好"; // 設(shè)置需要發(fā)送的消息。即設(shè)置$scope某個(gè)屬性的值。 // 觸發(fā)“發(fā)送按鈕”的點(diǎn)擊事件。 $(".action a").trigger("click");
非常好的方式。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ui-router中使用ocLazyLoad和resolve的具體方法
這篇文章主要介紹了ui-router中使用ocLazyLoad和resolve的具體方法,詳細(xì)的介紹了ocLazyLoad和resolve的具體用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10詳解Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問題
這篇文章主要為大家介紹了Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10angular inputNumber指令輸入框只能輸入數(shù)字的實(shí)現(xiàn)
這篇文章主要介紹了angular inputNumber指令輸入框只能輸入數(shù)字的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Angularjs 實(shí)現(xiàn)分頁功能及示例代碼
這篇文章主要介紹了Angularjs 實(shí)現(xiàn)分頁功能及示例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09Angular設(shè)計(jì)模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化
這篇文章主要為大家介紹了Angular設(shè)計(jì)模式hierarchical?injector實(shí)現(xiàn)代碼復(fù)用模塊化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10ionic3實(shí)戰(zhàn)教程之隨機(jī)布局瀑布流的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于ionic3實(shí)戰(zhàn)教程之隨機(jī)布局瀑布流的實(shí)現(xiàn)方法,文中通過示例代碼和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Angular使用過濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫轉(zhuǎn)換功能示例
這篇文章主要介紹了Angular使用過濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫轉(zhuǎn)換功能,涉及AngularJS過濾器針對(duì)字符串轉(zhuǎn)換的簡(jiǎn)單使用技巧,需要的朋友可以參考下2018-03-03