来自控制器调用的 AngularJS 触发器指令 [英] AngularJS Trigger directive from controller call
本文介绍了来自控制器调用的 AngularJS 触发器指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想触发一个包含 jQuery 指令的 AngularJS 自定义指令.怎么做到呢?我在指令中阅读了关于发射函数的内容?
I want to trigger an AngularJS custom directive that contains jQuery instructions. How can it be done? I have read about emit function in the directive?
想法?
推荐答案
您可以使用服务在控制器和指令之间进行通信.
You can use a service to communicate between the controller and the directive.
服务可能如下所示:
app.service("directiveService", function() {
var listeners = [];
return {
subscribe: function(callback) {
listeners.push(callback);
},
publish: function(msg) {
angular.forEach(listeners, function(value, key) {
value(msg);
});
}
};
});
指令可以响应服务:
app.directive("jQueryDirective", function(directiveService) {
directiveService.subscribe(function(msg) {
// pretend this is jQuery
document.getElementById("example")
.innerHTML = msg;
});
return {
restrict: 'E'
};
});
只需替换我为 jQuery 操作所做的一切,您就应该拥有所需的东西.
Just substitute what I did for jQuery manipulation and you should have what you need.
这是一个工作小提琴:http://jsfiddle.net/jeremylikness/wqXYx/
这篇关于来自控制器调用的 AngularJS 触发器指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文