从AngularJS到Javascript的事件
如何从AngularJS广播事件,并在基本JS脚本中侦听此事件。多谢各位从AngularJS到Javascript的事件,javascript,angularjs,events,Javascript,Angularjs,Events,如何从AngularJS广播事件,并在基本JS脚本中侦听此事件。多谢各位 <body data-ng-app="AngularApp"> <script> //my custom native JS Script //normal Javscript function , can be called within AngularJS normally function someJsFunction(){ console.lo
<body data-ng-app="AngularApp">
<script>
//my custom native JS Script
//normal Javscript function , can be called within AngularJS normally
function someJsFunction(){
console.log('this is some JS function');
}
// some Event handling and listentning to be fired wihtin AngularJS
function Event(sender) {
this._sender = sender;
this._listeners = [];
}
Event.prototype = {
attach: function (listener) {
this._listeners.push(listener);
},
notify: function (args) {
var index;
for (index = 0; index < this._listeners.length; index += 1) {
this._listeners[index](this._sender, args);
}
}
};
//Event Handling Usage
var myCutstomEvent = new Event(this);
myCutstomEvent.attach( function() {
console.log('myCustomEvent Handler');
});
</script>
<div ng-controller="myCtrl">
<input type="button" ng-click="clickBtn()" value="clickme"/>
</div>
</body>
。。。
控制台将显示以下内容:
buttonClicked in Controller
this is someService
this is some JS function
myCustomEvent Handler
干杯我们需要一些代码。您想做什么?避免使用带有angularJS的Jquery专门用于编辑连接到带有angularJS的套接字的DOMI,我有一些带有basic JS的应用程序。所以当套接字出现时,我需要从js代码调用函数。
buttonClicked in Controller
this is someService
this is some JS function
myCustomEvent Handler