如果我有这样的JSON输入:
$scope.managers =
[
{
managerName : "Sam",
employeesList: [
{ employeeName: 'Joe', employeeCategory: 'Developer' },
{ employeeName: 'Bob', employeeCategory: 'Developer' }
]
}
]
我想将该管理器的所有employeeNames
绑定到一个逗号分隔的输入,如下所示:
<div ng-repeat="manager in managers">
<input type="text" ng-model="manager.employeesList.employeeName" ng-list />
</div>
这样我就可以进入
了约翰,鲍勃
在输入字段中。
这样做的最佳方式是什么?
(我无法更改JSON对象的结构)
答案 0 :(得分:0)
我会在ng-click="storeNames(nameInput, manager)
上创建一个按钮。
.split()
接受一个字符串,并在每次看到选择器时将其分成数组元素(在这种情况下是&#39;,&#39;)
http://www.w3schools.com/jsref/jsref_split.asp
$scope.nameInput = '';
$scope.managers = [
{
managerName: 'Billy Joe',
employeesList: [
{ employeeName: 'Joe', employeeCategory: 'Developer' },
{ employeeName: 'Bob', employeeCategory: 'Developer' }
]
}
];
$scope.storeNames = function(names, manager) {
if(names.length > 0) {
names.split(", ");
for(var x = 0; x < names.length; x++) {
manager.employeesList.employeeName[x] = names[x];
}
}
}