在AngularJS的controller外部直接获取$scope

为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处。LaplaceDemon/SJQ。

http://www.cnblogs.com/shijiaqi1066/p/5560843.html

 

 

 

以前利用webqq的写过一个自动发消息的脚本,由于那时webqq还直接使用类似jQuery操作DOM的技术,所以脚本很简单就可以实现。

现如今很多web应用都开始使用AngularJS,MVVM导致的就是无法操作dom而去直接改变数据。所以面对网页版微信,要实现一个自动发送的脚本,就无法再用以前那套DOM的思路了。


 

要修改AngularJS中的数据,首先就要获取scope。其实获取scope的方法很简单。

由于大部分Angular项目需要使用jQuery作为补充。可以在jQuery中通过.scope()方法获取当前选择器内容里继承的域。

即类似这样的方式:

$('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>

 

那么打开网页版微信的页面(2016-06-05)选中你需要发消息的人。然后执行如下脚本:

var controllerScope = $('div[ng-controller="chatSenderController"]').scope();    // 获取chatSenderController的$scope
controllerScope.editAreaCtn = "星哥,下午好";    // 设置需要发送的消息。即设置$scope某个属性的值。

//  触发“发送按钮”的点击事件。
$(".action a").trigger("click");

 

非常好的“外挂方式”。

 

 

 

为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处。LaplaceDemon/SJQ。

http://www.cnblogs.com/shijiaqi1066/p/5560843.html

转载于:https://www.cnblogs.com/shijiaqi1066/p/5560843.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值