[英]AngularJS - Directive to Directive function call
Directive One 指令一
myApp.directive("myDirective", function ($compile, $rootScope) {
var linker = function (scope, element, attrs)
{
var myEl3 = angular.element(document.querySelector("#mydiv" + objMode + Id));
myEl3.after($compile("<span id='fullscreen" + objMode + Id + "' ng-show='true' uib-tooltip='Full Screen' tooltip-placement='left' class='fullscreen text-right pad-0 font-10' ><button id='fullscren" + Id + "' ng-click=doFullScreen('" + vlcPlayerId + "')></button></span></span>")($rootScope));
//this function is used for vlcid.
scope.getVLC = function (name) {
if ($window.document[name]) {
return $window.document[name];
}
if ($window.navigator.appName.indexOf("Microsoft Internet") == -1) {
if ($window.document.embeds && $window.document.embeds[name])
return $window.document.embeds[name];
} else {
return $window.document.getElementById(name);
}
}
//this function is used for fullscreen.
scope.doFullScreen = function (vlcPlayer) {
var vlc = scope.getVLC(vlcPlayer);
if (vlc) {
vlc.video.toggleFullscreen();
}
}
};
return {
restrict: "E",
link: linker
};
});
Directive Two 指令二
myApp.directive("myNewDirective", function ($compile, $rootScope) {
var linker = function (scope, element, attrs)
{
scope.getVLC = function (name) {
if ($window.document[name]) {
return $window.document[name];
}
if ($window.navigator.appName.indexOf("Microsoft Internet") == -1) {
if ($window.document.embeds && $window.document.embeds[name])
return $window.document.embeds[name];
} else {
return $window.document.getElementById(name);
}
}
scope.doFullScreen = function (vlcPlayer) {
var vlc = scope.getVLC(vlcPlayer);
if (vlc) {
vlc.video.toggleFullscreen();
}
}
};
return {
restrict: "E",
link: linker
};
});
How do I call scope.doFullScreen
defined in myNewDirective
to myDirective
so that I can reuse the same function and avoid declaring same function in two directives? 如何将
scope.doFullScreen
定义的myNewDirective
调用为myDirective
以便可以重用相同的函数并避免在两个指令中声明相同的函数?
PS: I do not want to declare the function in service and inject in directive. PS:我不想在服务中声明函数并在指令中注入。
You can declare this function in a service and add it with dependency injection, it would be the cleaner way. 您可以在服务中声明此函数,并通过依赖注入将其添加,这将是一种更简洁的方法。
myApp.service("directiveService", function ($compile, $rootScope) {
var linker = function (scope, element, attrs)
{
scope.getVLC = function (name) {
if ($window.document[name]) {
return $window.document[name];
}
if ($window.navigator.appName.indexOf("Microsoft Internet") == -1) {
if ($window.document.embeds && $window.document.embeds[name])
return $window.document.embeds[name];
} else {
return $window.document.getElementById(name);
}
}
scope.doFullScreen = function (vlcPlayer) {
var vlc = scope.getVLC(vlcPlayer);
if (vlc) {
vlc.video.toggleFullscreen();
}
}
};
return {
linker: linker
};
});
And you'll keep your directives simple : 而且您将使指令保持简单:
myApp.directive("myNewDirective", function (directiveService) {
return {
restrict: "E",
link: directiveService.linker
};
});
Hope this helps ! 希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.