js和angularjs之间的相互调用

方法一

var element = angular.element($document.getElementById("app"));
var controller = element.controller();
var scope = element.scope();
//调用方法
scope.sub1();

//如果需要重新绑定
scope.$apply();

//调用字段
scope.field1;

angular.element(domElement).scope() to get the current scope for the element
angular.element(domElement).injector() to get the current app injector
angular.element(domElement).controller() to get a hold of the ng-controller instance.

方法二:通过controller来获取app
var appElement = document.querySelector('[ng-controller=mainController]');

然后在获取$scope变量:

var $scope = angular.element(appElement).scope(); 

如果改变了其中的变量之后,需要在页面表现出来,还需要调用apply()方法:

$scope.$apply();

参考文章:
https://segmentfault.com/a/1190000000747708
https://www.cnblogs.com/CheeseZH/p/4515638.html



posted @ 2018-05-25 11:08  miny_simp  阅读(501)  评论(0编辑  收藏  举报