Method 1: Get it through the controller
var appElement = document.querySelector('[ng-controller=mainController]'); // Get the $scope variable:
var $scope = angular.element(appElement).scope(); // 如果先调用Controller 里面的方法的话$scope.somefunc('xxx'); // 改变了值之后还需要调用一下下面的方法, 才可以刷新最新改变的内容$scope.$apply()
Method 2: Obtaining through DOM manipulation
//Get the app object through DOM operation var element = angular.element($document.getElementById("app")); // app is the value of the id of the DOM element where ng-app is located //Get the app object, you can get the app the controller var controller = element.controller(); //Get the app object, you can get the $scope of the app var scope = element.scope(); //call the method in $scope
…
The post js calling angularJs methods first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9697
This site is for inclusion only, and the copyright belongs to the original author.