我正在尝试简单地获取上一个值,并从下拉列表中获取新选择的值。在此示例中,下拉列表预先填充了分配用户的当前组。
当下拉列表发生变化时,我希望能够返回旧值和新值。我已经可以获得旧值,但我不知道如何返回新值。
控制器代码:
// User Object
userAccess = [{"user_name":"dodsosr11",
"group_level":1,
"user_id":500,
"group_id":10,
"group_name":"Conferences_Admins"},
{"user_name":"keatikj09",
"group_level":1,
"user_id":250,
"group_id":10,
"group_name":"Conferences_Admins"},
{"user_name":"malinag10",
"group_level":1,
"user_id":492,
"group_id":10,
"group_name":"Conferences_Admins"}];
//Group Object
groupAccess = [{"group_name":"Conferences_Admins",
"id":10,
"level_id":1},
{"group_name":"ticket_sales",
"id":59,
"level_id":3},
{"group_name":"Web Developers",
"id":1,
"level_id":1}];
$scope.reassignUser = function(){
var oldGroup = this.user.group_id;
var newGroup = ?????
};
HTML:
<tr ng-repeat="user in userAccess">
<td>{{ user.user_name}}</td>
<td>
<select ng-change="reassignUser()"
ng-model="user.group_name"
ng-options="g.group_name as g.group_name for g in groupAccess">
</select>
</td>
</tr>
我在这里使用下面第一个响应中提供的手表示例创建了一个小提琴。 http://jsfiddle.net/LHz9D/1/
答案 0 :(得分:13)
试试这个
查看强>
<select ng-model="selected" ng-change="change(selected, {{selected}})">
<强> JS 强>
$scope.change = function(newObj, oldObj){
console.log(newObj);
console.log(oldObj);
}
答案 1 :(得分:10)
您可以使用ng-click,因为在更改之前发生了单击,您可以像这样注册旧值
<select ng-model="selected" ng-change="change(selected, oldValue)" ng-click="oldValue = selected">
并在控制器中
$scope.change = function(newValue, oldValue){
//do if's in case you use null or empty values
if ( oldValue ) {
//do something
}
if ( newValue ) {
//do something
}
}
答案 2 :(得分:0)
这样做:
$scope.$watch('user.group_name', function(newValue, oldValue){
});
无需再使用ng-change