首页 > angularjs的$scope如何通过外部function操作?

angularjs的$scope如何通过外部function操作?

页面中同时引用了两个js文件,一个是angularjs的control,一个是常规js

control:

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

常规js:

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

自己google解决了,http://www.cnblogs.com/czcz1024/p/3808345.html,浪费资源了,sorry。


将angular的controller写在立即执行的函数表达式里:

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

然后再常规js中引入

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

善用google搜索,这种经常会遇到的问题,一般StackOverflow上都有的
How to access the angular $scope variable in browser's console?;

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

最土解决方案……window['scope']=$scope

【热门文章】
【热门文章】