ng-change obtener nuevo valor y valor original


Estoy usando ng-options para seleccionar valores de un pulldown. Me gustaría poder comparar el valor antiguo con el nuevo valor. ng-change funciona bien para agarrar el nuevo valor del pull down, pero ¿cómo puedo obtener tanto el nuevo valor como el valor original?

<select ng-change="updateValue(user)" ng-model="user.id" ng-options="user.id as user.name for user in users"></select> 

Por ejemplo, digamos que quería que el controlador para registrar, " Su ex user.name era BILL, tu nombre de usuario actual es PHILLIPE."

Author: Bailey Smith, 2014-10-29

6 answers

Con una {{expresión}} angular puede agregar el usuario anterior o user.id valor para el atributo ng-change como una cadena literal:

<select ng-change="updateValue(user, '{{user.id}}')" 
        ng-model="user.id" ng-options="user.id as user.name for user in users">
</select>

En ngChange, el 1er argumento para updateValue será el nuevo valor de usuario, el 2do argumento será el literal que se formó cuando la etiqueta select fue actualizada por última vez por angular, con el antiguo user.id valor.

 247
Author: db-inf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-09 19:30:20

Simplemente mantenga una variable currentValue en su controlador que actualice en cada cambio. A continuación, puede compararlo con el nuevo valor cada vez antes de actualizarlo.'

La idea de usar un reloj también es buena, pero creo que una variable simple es la solución más simple y lógica.

 12
Author: TGH,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-29 01:46:52

Puedes usar algo como ng-change = someMethod ({{user.id}}). Al mantener su valor en side {{expression}}, evaluará la expresión en línea y le dará el valor actual (valor antes de que se llame al método ng-change).

<select ng-model="selectedValue" ng-change="change(selectedValue, '{{selectedValue}}')">
 10
Author: Vivek Panday,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-01 09:35:13

También puedes usar

<select ng-change="updateValue(user, oldValue)"     
       ng-init="oldValue=0"
       ng-focus="oldValue=user.id"
       ng-model="user.id" ng-options="user.id as user.name for user in users">
</select>
 10
Author: Umut Catal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-02 07:27:41

Puede usar un reloj de alcance:

$scope.$watch('user', function(newValue, oldValue) {
  // access new and old value here
  console.log("Your former user.name was "+oldValue.name+", you're current user name is "+newValue.name+".");
});

Https://docs.angularjs.org/api/ng/type/ro rootScope. Scope#watch watch

 8
Author: tommybananas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-29 03:26:35

Podría usar un reloj en su lugar, porque tiene el valor antiguo y el nuevo, pero luego está agregando al ciclo de resumen.

Simplemente mantendría una segunda variable en el controlador y la establecería.

 2
Author: wbeange,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-29 01:01:26