angular.js - angularjs的$scope如何通过外部function操作?

浏览:28日期:2023-02-05

问题描述

页面中同时引用了两个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。

问题解答

回答1:

善用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();回答2:

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

回答3:

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

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

然后再常规js中引入

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

相关文章: