让两个自定义指令在AngularJS中相互交互
Have two custom directives interact with each other in AngularJS
我有两个指令
angular.module('myApp.directives', []).
directive('exampleDirective', ['version', function(version) {
return {
link:function(scope,elm,attr) {
elm.on('click',function() {
//access exampleDirective2 behaviour
});
}
}
}]).
directive('exampleDirective2', ['version', function(version) {
return {
link:function(scope,elm,attr) {
elm.on('change',function() {
//access exampleDirective behaviour
});
}
}
}]);
正如您在exampleDirective elm.on(click)函数上看到的,我想获得exampleDirective2函数,反之亦然。
在AngularJS中有办法实现这个吗?
这个问题有三个解决方案:
第一个解决方案:使用服务在指令之间共享一个服务,该服务可以包含数据和函数。
.service('myService', function(){
this.data = //....
this.function = function() //...
})
.directive('dir1', ['myService', function(myService) {
//...
link: function(scope, elem, attrs) {
scope.data = myService.data;
}
}])
其他指令也一样。
第二个解决方案:指令的控制器如果你的指令有父/子/兄弟关系:
.directive('dir1', function(){
return {
controller: function(scope, elem, attrs) {
this.sayHello = function() {
alert('hello')
}
}
}
})
.directive('dir2', function(){
return {
require: '^dir1',
link: function(scope, elem, attrs, dir1Ctrl) {
dir1Ctrl.sayHello(); //will alert hello
}
}
})
然而,如果你的指令有独立的作用域,这将不起作用。此外,根据指令的关系(父/子或兄弟),require
属性的sintax会略有变化;你可以在AngularJS的指令文档中找到更多信息。
你还可以从指令作用域发出/广播事件,或者注入$rootScope
并将其用作事件总线:
.directive('dir1', function($rootScope){
return {
link: function(scope, elem, attrs) {
var emitEvent = function(){
$rootScope.$emit('somethingHappenedEvent', { /*you can pass optional data*/ });
}
emitEvent();
}
}
})
.directive('dir2', function($rootScope) {
return {
link: function(scope, elem, attrs) {
$rootScope.$on('somethingHappenedEvent', function(event) {
if(!event.defaultPrevented) {
//react to event here
}
})
}
}
})
您也可以使用正常的scope
而不是$rootScope
,但在这种情况下,您必须记住事件将在所有范围内向上/向下起泡(取决于使用$emit
或$broadcast
)。我更喜欢$rootScope
中的$emit
,因为它将是唯一能够捕获事件的作用域,并且也将相当快。
这样做的一种方法是使用带有控制器的包装器指令,如果使用require
,则可以在指令之间共享该指令。一个更简单、也许更好的解决方案(因为它不依赖于DOM)是使用一个公共服务来支持指令之间的通信。
如果你想在指令的方式这样做。这是答案
angular.module('myApp.directives', []).
directive('exampleDirective', ['version', function(version) {
return {
link:function(scope,elm,attr) {
elm.on('click',function() {
//access exampleDirective2 behaviour
scope.exampleDirective2Function();
});
scope.exampleDirectiveFunction = function (){
//write your code here
}
}
}
}]).
directive('exampleDirective2', ['version', function(version) {
return {
link:function(scope,elm,attr) {
elm.on('change',function() {
//access exampleDirective behaviour
scope.exampleDirectiveFunction();
});
scope.exampleDirective2Function= function (){
//write your code here
}
}
}
}]);
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- AngularJS:我可以跳过函数参数回调吗
- AngularJS UI路由器不能像ng路由器那样工作
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 如何包含特定于每个视图angularjs的javascript文件
- AngularJS:ng之后,重复$scope值未按预期更新
- AngularJS-在JSON选择器中使用变量名
- 在Windows 10中自动执行例行程序(主要与浏览器交互)
- $window.ga在AngularJS事件中未定义
- AngularJS-使用'true'属性
- 打开一个模态并将其链接到AngularJS中的指令
- angularjs路线过渡如何以交互方式设置动画
- 使用 ux-datagrid、angularJS 和嵌套交互
- 有没有办法直接在 Angularjs 中与控制器和作用域进行交互
- AngularJS模态允许与背景交互
- AngularJS控制器-与视图元素交互
- AngularJs的用户认证只在第二次交互时发生
- 让两个自定义指令在AngularJS中相互交互
- 如何用AngularJS创建富交互的web应用
- 如何在AngularJS中的不同控制器之间进行交互