angularjs 從外部改變controller內的資料

來源:互聯網
上載者:User

標籤:style   class   code   color   資料   cti   

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的用法,但可能會遇到需要這樣做的時候。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.