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

时间:2017-06-06 06:22:25

标签: angularjs

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

html(ui / hotkeys.ui.html)

<div ng-controller="AddManagerController as vm">

<button class="btn hotkey" ng-change="vm.changed($index)" ng-class="{'btn-danger': $index === 1, 'btn-info': $index === 2, 'btn-primary': $index === 3, 'btn-success': $index === 4, 'btn-warning': $index === 5, 'btn-inverse': $index === 6 }" ng-if="hotkeys !== null" ng-model="vm.credentials.groups[$index]" ng-repeat="hotkeys in vm.showGroups track by $index" tabindex="-1" type="button" style="margin-right: 4px" uib-btn-checkbox>
  <svg width="42" height="42" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <circle cx="20" cy="20" r="4" stroke="black" fill="white" ng-if="$index === 1 || $index === 3 || $index === 5" stroke-width="1"/>
    <circle cx="10" cy="10" r="4" stroke="black" fill="white" ng-if="$index ===4 || $index === 5 || $index === 6" stroke-width="1"/>
    <circle cx="10" cy="20" r="4" stroke="black" fill="white" ng-if="$index === 6" stroke-width="1"/>
    <circle cx="10" cy="30" r="4" stroke="black" fill="white" ng-if="$index === 2 || $index === 3 || $index === 4 || $index === 5 || $index === 6" stroke-width="1"/>
    <circle cx="30" cy="10" r="4" stroke="black" fill="white" ng-if="$index === 2 || $index === 3 || $index === 4 || $index === 5 || $index === 6" stroke-width="1"/>
    <circle cx="30" cy="20" r="4" stroke="black" fill="white" ng-if="$index === 6" stroke-width="1"/>
    <circle cx="30" cy="30" r="4" stroke="black" fill="white" ng-if="$index === 4 || $index === 5 || $index === 6" stroke-width="1"/>
  </svg>
</button>

</div>

这是我想从另一个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();
      }
    }


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

1 个答案:

答案 0 :(得分:1)

你有更多选择将JS与控制器分开(诚实地与另一个控制器分开)。如果你想要一些单例使用,你应该为它创建service。如果您想要一些没有单独视图的特殊功能,请创建filterdirective。如果您的特殊功能有一些视图(HTML),请创建component。这是angularJS basic

相关问题