Angularjs 将js与角度控制器分离(我是否为此创建指令?)

Angularjs 将js与角度控制器分离(我是否为此创建指令?),angularjs,Angularjs,我使用了很多内置的角度指令,比如html中的ng repeat。我已经使用ng include将UI组件分离到不同的HTML文件中。如何将js从控制器中分离出来 html(ui/hotkeys.ui.html) 这是我想从另一个js文件加载的代码,而不是将所有这些保存在控制器中 /* ============ HOTBAR ============ */ var enableHotbar = true; $document.bind('keypress', function (e) {

我使用了很多内置的角度指令,比如html中的ng repeat。我已经使用ng include将UI组件分离到不同的HTML文件中。如何将js从控制器中分离出来

html(ui/hotkeys.ui.html)


这是我想从另一个js文件加载的代码,而不是将所有这些保存在控制器中

/* ============ HOTBAR ============ */
var enableHotbar = true;
$document.bind('keypress', function (e) {
    //console.log(e.key);
    var key = String.fromCharCode(e.which);
    if (key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6) {
        //alert(key);
        $scope.$broadcast('keypress', e, e.which);
    }
});
$scope.keyList = []; //["1","2", "3"];
$scope.$on('keypress', function (event, key) {
  var easyToReadKeyFromKeycode = String.fromCharCode(key.charCode);
  if (enableHotbar && (document.activeElement.id !== 'manager-phone') && (document.activeElement.id !== 'manager-name')) {
    vm.credentials.groups.forEach(function(group, index){
      if (easyToReadKeyFromKeycode === (index).toString()) {
        vm.credentials.groups[index] = !vm.credentials.groups[index];
      }
    })
  }
  $scope.keyList.push(String.fromCharCode(key.charCode));
  //console.log($scope.keyList);
  $scope.$apply();
});

  // add ng-focused and ng-blurred to any input element requiring numbers
    $scope.focused = function (e) {
      enableHotbar = false;
    }
    $scope.blurred = function (e) {
      enableHotbar = true;
    }

    // Manager Name disables keys 1 through 6
    $scope.keyPressed = function(e) {
      console.log(e);
      if ((e.keyCode >= 49 && e.keyCode <= 54)) {
        event.preventDefault();
      }
    }


/* ================================= */
/*==============================================*/
var enableHotbar=true;
$document.bind('keypress',函数(e){
//控制台日志(e.key);
var key=String.fromCharCode(e.which);
如果(键==1 | |键==2 | |键==3 | |键==4 | |键==5 | |键==6){
//警报(键);
$scope.$broadcast('keypress',e,e.which);
}
});
$scope.keyList=[]//["1","2", "3"];
$scope.$on('keypress',函数(事件,键){
var easyToReadKeyFromKeycode=String.fromCharCode(key.charCode);
if(enableHotbar&&(document.activeElement.id!=='manager phone')&&(document.activeElement.id!=='manager name')){
vm.credentials.groups.forEach(函数(组,索引){
if(easyToReadKeyFromKeycode==(index.toString()){
vm.credentials.groups[index]=!vm.credentials.groups[index];
}
})
}
$scope.keyList.push(String.fromCharCode(key.charCode));
//log($scope.keyList);
$scope.$apply();
});
//将ng聚焦和ng模糊添加到任何需要数字的输入元素
$scope.focused=函数(e){
enableHotbar=false;
}
$scope.fuzzle=函数(e){
enableHotbar=true;
}
//管理器名称禁用键1到6
$scope.keyPressed=功能(e){
控制台日志(e);

如果((e.keyCode>=49&&e.keyCode您有更多的选项来将JS从控制器(到另一个控制器)分离。如果您想要一些单例使用,您应该为其创建。如果您想要一些没有单独视图的特殊功能,请创建or。如果您的特殊功能有一些视图(HTML)创建。这是angularJS basic

/* ============ HOTBAR ============ */
var enableHotbar = true;
$document.bind('keypress', function (e) {
    //console.log(e.key);
    var key = String.fromCharCode(e.which);
    if (key == 1 || key == 2 || key == 3 || key == 4 || key == 5 || key == 6) {
        //alert(key);
        $scope.$broadcast('keypress', e, e.which);
    }
});
$scope.keyList = []; //["1","2", "3"];
$scope.$on('keypress', function (event, key) {
  var easyToReadKeyFromKeycode = String.fromCharCode(key.charCode);
  if (enableHotbar && (document.activeElement.id !== 'manager-phone') && (document.activeElement.id !== 'manager-name')) {
    vm.credentials.groups.forEach(function(group, index){
      if (easyToReadKeyFromKeycode === (index).toString()) {
        vm.credentials.groups[index] = !vm.credentials.groups[index];
      }
    })
  }
  $scope.keyList.push(String.fromCharCode(key.charCode));
  //console.log($scope.keyList);
  $scope.$apply();
});

  // add ng-focused and ng-blurred to any input element requiring numbers
    $scope.focused = function (e) {
      enableHotbar = false;
    }
    $scope.blurred = function (e) {
      enableHotbar = true;
    }

    // Manager Name disables keys 1 through 6
    $scope.keyPressed = function(e) {
      console.log(e);
      if ((e.keyCode >= 49 && e.keyCode <= 54)) {
        event.preventDefault();
      }
    }


/* ================================= */