Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get old value and new value from dropdown

I'm trying to simply get the previous value, and the newly selected value from a drop-down. In this example, the drop-down is pre-populated with the current group the user is assigned.

When the drop-down is changed, I want to be able to return the old value and the new value. I can already get the old value, but I don't know how to return the new value.

Controller Code:

// 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>

I've created a fiddle here using the watch example that was provided in the first response below. http://jsfiddle.net/LHz9D/1/

like image 658
Photovor Avatar asked Feb 20 '14 13:02

Photovor


1 Answers

you could use ng-click, since click happens before change you could there register the old value like this

<select ng-model="selected" ng-change="change(selected, oldValue)" ng-click="oldValue = selected">

and in the controller

$scope.change = function(newValue, oldValue){
    //do if's in case you use null or empty values
    if ( oldValue ) {
        //do something
    }   
    if ( newValue ) {
        //do something
    }
}
like image 52
bresleveloper Avatar answered Sep 28 '22 04:09

bresleveloper