Call AngularJS from other scripts

后端 未结 1 613
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 10:44

application. Now i want to implement an dynamic menu with AngularJS. Therefore i need to change variables in the AngularJS application from my existing application.

相关标签:
1条回答
  • 2020-12-11 11:39

    Just attach a selector to your DOM element where your controller is defined. like

    <div ng-app="myApp" ng-controller="myCtrl" id="myCtrl">
    

    and from anywhere you can call this controller function like

    angular.element('#myCtrl').scope().resetName()
    

    OR

    angular.element(document.querySelector('#myCtrl')).scope().resetName()
    

    In some case you need to modify a object value of controller you can do it in simple way. Just use

    angular.element(document.querySelector('#myCtrl')).scope().title = 'test';
    

    Note : Please don't forget to apply the changes because now angular will not trigger apply automatically. You need to trigger apply manually. Just put below line after updating values in object/s

    angular.element(document.querySelector('#myCtrl')).scope().apply();
    
    0 讨论(0)
提交回复
热议问题