所以,我在一个有角度的重复项目中有一个选择输入。我想用一个按钮为一个人分配房间。除了维持一个人选择了房间外,它还会显示它已在选择输入中分配。
这是我的Firebase数据:
{
"-JLtuRmYiTDLTnZPj" : {
"last_name" : "NameOne",
"person_id" : 1,
"is_in" : false
},
"-JLtuRBlyWhHV28zd" : {
"last_name" : "NameTwo",
"person_id" : 2,
"is_in" : false
},
"-JLtuQxlMn25APuKT" : {
"last_name" : "NameThree",
"person_id" : 3,
"is_in" : false
}
}
这是我的代码:
<table>
<tbody>
<tr ng-repeat="(name, person) in people | orderBy: 'doctor_id'" ng-model="person">
<td>{{person.person_id}}</td>
<td>{{person.last_name}}</td>
<td><strong>{{person.is_in}}</strong></td>
<td>Assign Rooms: <select multiple class="form-control" ng-model="person.room_id" ng-options="r.room_id as r.name for (key, r) in rooms"></select></td>
<td><button type="button" href="" class="btn btn-primary" ng-click="assignRooms(name)">Assign Rooms</button></td>
</tr>
</tbody>
</table>
最后我的JS:
angular.module('app', ['ngRoute', 'firebase']);
$scope.people = $firebase(new Firebase("https://<FB-NAME>.firebaseio.com/dev/0/people"));
$scope.rooms = $firebase(new Firebase("https://<FB-NAME>.firebaseio.com/dev/0/rooms"));
//Assigns Rooms to each doctor.
$scope.assignRooms = function(name) {
$scope.people.$child(name).$update({
"room_id": $scope.person.room_id,
"is_in": true
});
}
正确调用该应用。当我使用Angular JS Batarang时,我可以看到范围。当我单击Assign Rooms按钮时,我收到错误:
无法阅读属性&#39; room_id&#39;未定义的
来自:$ scope.person.room_id。
我确信这是我想念的事情。但是非常感谢任何帮助。
谢谢!
答案 0 :(得分:1)
您需要使用person
。您无法访问函数中的ng-repeat范围。
<td>
<button type="button"
href=""
class="btn btn-primary"
ng-click="assignRooms(name,person)">Assign Rooms</button>
</td>
$scope.assignRooms = function(name, person) {$scope.assignRooms = function(name) {
$scope.people.$child(name).$update({
"room_id": person.room_id,
"is_in": true
});
}