首页 > 解决方案 > 如何在angularjs中的多个控制器中使用相同的条件

问题描述

我需要在多个控制器中使用 if 条件。而不是一次又一次地写,我怎么能以一种简单的方式来做。

主控制器.js:

 $scope.responseData.forEach(function(card) {

    if (card.Category == 'Apple') {
         console.log("Apple");
       } else if (card.Category == 'Google') {
         console.log("Google");
       } else if (card.Category == 'Microsoft') {
         console.log("Microsoft");
       } else if (card.Category == 'Amazon') {
         console.log("Amazon");
    }
});

FirstController.js:

$scope.responseData.forEach(function(card) {
    if (card.Category == 'Apple') {
         console.log("Apple");
       } else if (card.Category == 'Google') {
         console.log("Google");
       } else if (card.Category == 'Microsoft') {
         console.log("Microsoft");
       } else if (card.Category == 'Amazon') {
         console.log("Amazon");
    }
});

SecondController.js:

$scope.responseData.forEach(function(card) {

    if (card.Category == 'Apple') {
         console.log("Apple");
       } else if (card.Category == 'Google') {
         console.log("Google");
       } else if (card.Category == 'Microsoft') {
         console.log("Microsoft");
       } else if (card.Category == 'Amazon') {
         console.log("Amazon");
    }

});

像这样我如何在一个地方使用这个条件并在许多控制器中使用它。

标签: javascriptangularjs

解决方案


如果我正确理解你的问题,你可以做到这一点的一种方法是使用工厂。我在这里写了一个例子:https ://jsbin.com/veregejawo/1/edit?html,js,console,output 我希望这对你有帮助: HTML:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="app">

<div ng-controller="mainController">
  {{controller}}
  <br>
  <button ng-click="testfactory('Apple')">Apple</button>
</div>

<div ng-controller="firstController">
  {{controller}}
  <br>
  <button ng-click="testfactory('Google')">Google</button>
</div>

<div ng-controller="secondController">
  {{controller}}
  <br>
  <button ng-click="testfactory('Microsoft')">Microsoft</button>
</div>

<script>

</script>

</body>
</html>

控制器:

angular.module('app', []);
angular.module('app').factory('ifFactory', function () {

    var factory = {};
    factory.checkIf = function (param) {
        if (param == 'Apple') {
           console.log("Apple");
         } else if (param == 'Google') {
           console.log("Google");
         } else if (param == 'Microsoft') {
           console.log("Microsoft");
         } else if (param == 'Amazon') {
           console.log("Amazon");
         }
    };
    return factory;

});

angular.module('app').controller('mainController', ['$scope', 'ifFactory',
    function ($scope, ifFactory) {
      $scope.controller = "mainController";

      $scope.testfactory = function(param){
        ifFactory.checkIf(param);
      }
    }
]);

angular.module('app').controller('firstController', ['$scope', 'ifFactory',
    function ($scope, ifFactory) {
      $scope.controller = "FirstController";

      $scope.testfactory = function(param){
        ifFactory.checkIf(param);
      }
    }
]);

angular.module('app').controller('secondController', ['$scope', 'ifFactory',
    function ($scope, ifFactory) {
      $scope.controller = "SecondController";

      $scope.testfactory = function(param){
        ifFactory.checkIf(param);
      }
    }
]);

推荐阅读