angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngModel's $pristine state not updated when changes come from model

Open cxyokk opened this issue 13 years ago • 4 comments

Hi.

In angular's src here, it seems that ngModel's $pristine state only changes when $setViewValue() is invoked. Since $setViewValue() is only invoked when changes come from view (e.g. when users type in the input box), $pristine state is not updated when changes come from model (as in here).

My use case:

<input type="text" ng-model="data.a" />
<input type="text" ng-model="data.b" ng-change="doSomething()" />
$scope.doSomething = function() {
    $scope.a = $scope.b; // changes "$scope.a" from model
};

When user types in the second input, the first input's content changes correspondingly. This is correct. But the first input's $pristine state is not updated (still $pristine=true).

Is this something we need to fix? Or am i misunderstanding the meaning of $pristine?

Huge thanks for creating angular!

cxyokk avatar Aug 02 '13 11:08 cxyokk

$pristine means that a user didn't interact with a given control through UI yet. It is not meant to change on model mutations.

pkozlowski-opensource avatar Aug 02 '13 11:08 pkozlowski-opensource

It's werid!

leonfancy avatar Aug 02 '13 11:08 leonfancy

@pkozlowski-opensource Thanks for answering. But what if i wanna know if some kind of indirect interactions with a control has happened, like in my use case? If $pristine is not meant for that, does angular have some builtin states for that?

cxyokk avatar Aug 02 '13 13:08 cxyokk

@cxyokk no, but you can easily achieve this by $watching your model.

btford avatar Jan 11 '14 00:01 btford