javascript - 如何在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");
}
});
像这样我如何在一个地方使用这个条件并在许多控制器中使用它。
解决方案
如果我正确理解你的问题,你可以做到这一点的一种方法是使用工厂。我在这里写了一个例子: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);
}
}
]);
推荐阅读
- c++ - 为什么在某些情况下,自由函数不能开始对 C 数组进行操作,而 std::begin 可以在 C++14 中操作?
- authentication - 如何在新的 .Net Core 项目中使用现有的 ASP.NET 成员数据?
- php - 如何将子项添加到下拉菜单并将它们链接到其父项
- javascript - 从对象数组中收集键并将其减少为单个数组并删除重复项
- asp.net-core - 什么可能导致 JwtSecurityTokenHandler.ValidateToken 抛出“IDX10503:签名验证失败”。密钥何时有效?
- html - 使用相对属性的类不正确
- apache-kafka - 将Kafka 0.8.2.1集群中的数据镜像到Kafka 2.2.0集群
- python - Keras LSTM 问题,如何正确设置时间序列的神经网络?
- python - 使用正则表达式过滤 DataFrame 列
- sql - SQL Server 使用预定义的顺序进行排序