我的范围内有以下数据模型。
$scope.staff = [
{"id": "1","name": "Alex"},
{"id": "2","name": "Brad"},
{"id": "3","name": "Cam"}
];
$scope.closedShop =[
{"day": "monday","staffID": "1"},
{"day": "wednesday","staffID": "2"},
{"day": "thursday","staffID": "3"},
{"day": "friday","staffID": "1"}
];
我想要做的是输出哪个工作人员每天关闭商店,所以基于上面我的理想输出将是。
亚历克斯星期一关闭了商店 布拉德星期三关闭了商店Cam周四关闭了商店
亚历克斯星期五关闭了商店我遇到的问题是closedShop数据只包含staffID,我怎样才能根据staff.id数据来查找这个人名?
我在下面有一个工作示例,但对于少数员工来说,这不是一个可扩展的解决方案吗?
'use strict';
var app = angular.module('app', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.staff = [
{"id": "1","name": "Alex"},
{"id": "2","name": "Brad"},
{"id": "3","name": "Cam"}
];
$scope.closedShop =[
{"day": "monday","staffID": "1"},
{"day": "wednesday","staffID": "2"},
{"day": "thursday","staffID": "3"},
{"day": "friday","staffID": "1"}
];
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MainCtrl">
<div ng-repeat="closed in closedShop">
<span ng-if="staff[0].id==closed.staffID">{{staff[0].name}}</span>
<span ng-if="staff[1].id==closed.staffID">{{staff[1].name}}</span>
<span ng-if="staff[2].id==closed.staffID">{{staff[2].name}}</span>
closed the shop on {{closed.day}}
</div>
</div>
</div>
&#13;
让这项工作最好的方法是什么?
答案 0 :(得分:4)
这部分是钱:
<span ng-init="person = (staff | filter:{id:closed.staffID}:true)[0]">{{person.name}}</span>
这可能是不言自明的,但我会详细说明它有所帮助。
{{person.name}}
开始。 person
,因此我们需要使用ng-init
来创建变量closed.staffID
。 ( )
。参数设置为true,以便我们只获取id匹配的结果。由于过滤语法,此部分需要包含在[0]
中。person
,以便将 var app = angular.module('app', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.staff = [
{"id": "1","name": "Alex"},
{"id": "2","name": "Brad"},
{"id": "3","name": "Cam"}
];
$scope.closedShop =[
{"day": "monday","staffID": "1"},
{"day": "wednesday","staffID": "2"},
{"day": "thursday","staffID": "3"},
{"day": "friday","staffID": "1"}
];
}]);
设置为匹配对象,该对象将是数组中的第一个项目(项目0)。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MainCtrl">
<div ng-repeat="closed in closedShop">
<span ng-init="person = (staff | filter:{id:closed.staffID}:true)[0]">{{person.name}}</span>
closed the shop on {{closed.day}}
</div>
</div>
</div>
&#13;
ng-init="person = getPerson(closed.staffID)"
&#13;
您也可以执行app.controller('MainCtrl', ['$scope', 'filterFilter', function ($scope, filter) {
并将过滤逻辑移动到控制器中。为此,您将注入过滤器过滤器(有趣的名称),如下所示:
$scope.getPerson = function(id) {
return filter($scope.staff, {id:id}, true)[0];
};
然后使用此功能:
{{1}}
答案 1 :(得分:2)
您可以添加到控制器功能以获取员工ID。
$scope.getStaff = function(id){find and return}
并更改您的观点:
<div ng-app="app">
<div ng-controller="MainCtrl">
<div ng-repeat="closed in closedShop">
<span>{{getStaff(closed.staffID)}}</span>
closed the shop on {{closed.day}}
</div>
</div>
</div>
答案 2 :(得分:1)
可能这不是你所期望的,但是能够正常工作:
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MainCtrl">
<div ng-repeat="closed in closedShop">
<span ng-repeat="s in staff" ng-if="s.id==closed.staffID">
{{s.name}}
</span>
closed the shop on {{closed.day}}
</div>
</div>
</div>
<script src="app.js"></script>
<强> JS:强>
var app = angular.module('app', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
$scope.staff = [
{"id": "1","name": "Alex"},
{"id": "2","name": "Brad"},
{"id": "3","name": "Cam"}
];
$scope.closedShop =[
{"day": "monday","staffID": "1"},
{"day": "wednesday","staffID": "2"},
{"day": "thursday","staffID": "3"},
{"day": "friday","staffID": "1"}
];
}]);
请参阅此plunkr