How do I create a custom event in an AngularJs service

前端 未结 3 755
孤独总比滥情好
孤独总比滥情好 2020-12-05 00:37

I am working on an AngularJs project. I have a service which sets and removes events on some buttons. This service is utilized by another service which I do not want to inte

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 00:45

    You can create and emit an event with the following on your element

    ng-click="$emit('customEvent')"
    

    Then in your controller you can use

    $rootScope.$on('customEvent', function(){
        // do something
    })
    

    Demo

提交回复
热议问题