Angular Directive适用于所有控制器

时间:2016-09-10 20:01:30

标签: angularjs controller key directive

我有以下指令:

app.directive('remoteDirective', ['$location', 'playlistService', 'videoService', function ($location, playlistService, videoService) {
return {
    restrict: 'A',
    link: function (scope, el, attrs) {
        $('body').on('keydown', function (evt)
        {
            if (evt.keyCode === 38) //Up Arrow
            {
                scope.currentChannel.number++;
                $('#modal1').openModal();
                videoService.playVideo(playlistService.getChannel(scope.currentChannel.number).file);
                scope.currentChannel.name = scope.playlist[scope.currentChannel.number].title;
                scope.$apply();
            } 
            else if (evt.keyCode === 40) //Down Arrow
            {
                scope.currentChannel.number--;
                $('#modal1').openModal();
                videoService.playVideo(playlistService.getChannel(scope.currentChannel.number).file);
                scope.currentChannel.name = scope.playlist[scope.currentChannel.number].title;
                scope.$apply();
            } 
            else if (evt.keyCode === 13) //Enter
            {
                $('.button-collapse').sideNav('show');
            }
            else if (evt.keyCode === 27) //Esc
            {
                $location.path('settings');
                scope.$apply();
            }
        });
    }
};

}]);

我在我的主控制器中使用它

<div class="valign-wrapper" style="width: 100%; height: 100%" remote-directive >...</div>
问题是,这个指令在所有控制器中触发,不仅在我的主要......如何解决?我想在其他控制器中为theese键做另一个动作......

0 个答案:

没有答案