0

js 调用 angularJs 的方法

 1 year ago
source link: https://blog.p2hp.com/archives/9697
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

方法一:通过controller来获取

var appElement = document.querySelector('[ng-controller=mainController]');

// 获取$scope变量:
var $scope = angular.element(appElement).scope(); 

// 如果先调用 Controller 里面的方法的话

$scope.somefunc('xxx');

// 改变了值之后还需要调用一下下面的方法, 才可以刷新最新改变的内容

$scope.$apply()

方法二:通过DOM操作获取

//通过DOM操作获取app对象
var element = angular.element($document.getElementById("app")); // app 是ng-app所在的DOM元素的id的值
//得到app对象,可以获取app的controller
var controller = element.controller();
//得到app对象,可以获取app的$scope
var scope = element.scope();
//调用$scope中的方法
scope.sub1();
//调用方法后,可以重新绑定,在页面同步(可选)
scope.$apply();
//调用字段
scope.field1;

转载: https://www.orchome.com/1319


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK