var appElement = document.querySelector('[ng-controller=seatsCtrl]'); var $scope = angular.element(appElement).scope(); $scope.$apply(function () { $scope.data = [ { id: 1, cnt: 4 }, { id: 3, cnt: 5 }, { id: 2, cnt: 6 } ]; });
取到scope,然后調用$apply
或者在controller里寫一個function
$scope.update = function (data) { $scope.data = data; $scope.$apply(); }
外部調用這個function
var ele = document.querySelector("[ng-controller=seatsCtrl]"); angular.element(ele).scope().update([ { id: 1, cnt: 7 }, { id: 3, cnt: 8 }, { id: 2, cnt: 9 } ]);
注意function內部,需要調用$apply()
如果不調用apply,雖然scope的數據變化了,但並不會更新到頁面上去顯示
從非angularjs的js代碼中去改變angularjs的controller的數據,或許,這並不是標准的angularjs的用法,但可能會遇到需要這樣做的時候。
