【发布时间】:2017-06-06 20:05:27
【问题描述】:
(function () {
'use strict';
angular
.module('contacts.directives')
.directive('hotkeys', HotkeysDirective);
HotkeysDirective.$inject = ['$resource', '$document'];
function HotkeysDirective($resource, $document) {
var directive = {
templateUrl: '/my-app/contacts/client/views/ui/hotkeys.ui.html',
link: link,
controller: 'AddManagerController',
controllerAs: 'vm',
scope: {},
bindToController: {
},
};
return directive;
function link(scope, element, attrs, modelCtrl) {
$document.bind('keypress', function (e) {
var key = String.fromCharCode(e.which);
if (key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6) {
scope.$broadcast('keypress', e, e.which);
}
});
scope.$on('keypress', function (event, key) {
// do stuff
scope.$apply();
});
}
}
}());
这是我认为不起作用的代码...
HotkeysDirective.$inject = ['$resource', '$document'];
function HotkeysDirective($resource, $document) {
var directive = {
templateUrl: '/request-off-work/contacts/client/views/ui/hotkeys.ui.html',
link: link,
controller: 'AddManagerController',
controllerAs: 'vm',
scope: {},
bindToController: {
},
};
return directive;
function link(scope, element, attrs, modelCtrl) {
当我使用controllerAs:'vm'时,它说vm未定义,在AddManagerController中,它说'var vm = this;'。也不知道为什么 scope: {} 需要在那里,我最终会弄清楚 bindToController ......我想我需要将 vm.credentials.groups 绑定到它。
更新:这是控制器
function AddManagerController($scope, $document, $rootScope, $state, $window, Authentication, ContactsService, Notification) {
var vm = this;
vm.authentication = Authentication;
vm.credentials = {};
vm.showGroups = [null, false, false, false, false, false, false];
【问题讨论】:
标签: angularjs angular-directive