从控制器调用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);
});
}
};
});
和指令可以到服务响应:
And the directive could respond to the service:
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/
Here's a working fiddle: http://jsfiddle.net/jeremylikness/wqXYx/
这篇关于从控制器调用AngularJS触发指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文