Access Angular object's function from outside JS

后端 未结 2 716
醉梦人生
醉梦人生 2020-12-17 02:49

I\'m building a HTML-app with the AngularJS framework. I have some legacy JavaScript actions that needs to access a function within the Angular-object, and I can\'t get it t

2条回答
  •  温柔的废话
    2020-12-17 03:14

    angular.element() expects a DOM element, so if you have this html:

    and you want to access its DOM element, use an id:

    Then

    angular.element($('#myDiv')).scope().info('me')
    

    or without jQuery:

    angular.element(document.getElementById('myDiv')).scope().info('me')
    

    should work.

    Edit:

    If you changed something on the scope, you will probably need to use $apply():

    angular.element(document.getElementById('myDiv')).scope().$apply();
    

提交回复
热议问题