zoukankan      html  css  js  c++  java
  • 在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

  • 相关阅读:
    常用的字符串内建函数(三)
    常用的字符串内建函数(二)
    常用的字符串内建函数(一)
    Python 运算符
    Python标准数据类型--数字类型
    HTTPS及免费证书获取
    try...catch...finally
    加密和SSH认证
    全表更新锁表
    秩和比
  • 原文地址:https://www.cnblogs.com/shijiaqi1066/p/5560843.html
Copyright © 2011-2022 走看看