我正在尝试使用过滤器
过滤字符串数组var employeeRole=['Manager', 'Employee', 'Director', 'Supervisor', 'TeamLead'];
var permittedRoles='Manager|Director';
如何在permittedRoles
employeeRole
中的任何一个
答案 0 :(得分:0)
您可以splitting permittedRoles
|
执行此操作并检查结果数组中是否包含任何值(使用some)(使用employeeRole
中的indexOf}。
见下面一个完整的例子:
angular
.module('app', [])
.controller('ctrl', function($scope) {
$scope.employeeRole = ['Manager', 'Employee', 'Director', 'Supervisor', 'TeamLead'];
$scope.permittedRoles = 'Manager|Director';
$scope.check = function() {
console.log(doCheck())
};
/**
* Return `true` if any of the values in `permittedRoles` separated by `|` is contained in the array `employeeRole`, `false` otherwise.
*/
function doCheck() {
return $scope.permittedRoles.split('|')
.some(
function(role) {
return $scope.employeeRole.indexOf(role) !== -1;
}
);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<label for="permittedRoles">permittedRoles: </label>
<input id="permittedRoles" ng-model="permittedRoles" />
<hr/>
<a href="#" ng-click="check()">Check (click me!)</a>
<hr/>
<label>employeeRole:</label>
<br/>
<div ng-repeat="role in employeeRole">
{{role}}
</div>
</div>