search

Home  >  Q&A  >  body text

How is angularjs's $scope manipulated by external functions?

Two js files are referenced in the page at the same time, one is the control of angularjs, and the other is regular js

control:

javascriptvar app = angular.module('app', []);
app.controller('ctrl', function($scope) {
    $scope.aaa = 1;
});

Regular js:

javascript(function() {
    $scope.aaa = 2;  //有什么办法可以让这个实现?
})();

I solved it by google, http://www.cnblogs.com/czcz1024/p/3808345.html,浪费资源了,sorry。

高洛峰高洛峰2776 days ago526

reply all(3)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 16:53:37

    Make good use of Google search. This kind of problem you often encounter is usually found on StackOverflow
    How to access the angular $scope variable in browser's console?;

    javascriptangular.element('[ng-controller=ctrl]').scope();
    angular.element(document.querySelector('#id')).scope();
    

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-15 16:53:37

    The most rustic solution...window['scope']=$scope

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 16:53:37

    Write the angular controller in the function expression that is executed immediately:

        var app = angular.module('app', []);
        app.controller('ctrl', function($scope) {
            $scope.aaa = 1;
        });
    

    Then introduce it into regular js

    (function(app) {
        $scope.aaa = 2;  //有什么办法可以让这个实现?
    })(app);
    

    reply
    0
  • Cancelreply