Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reseting input fails #18

Open
elincognito opened this issue Mar 3, 2015 · 7 comments
Open

Reseting input fails #18

elincognito opened this issue Mar 3, 2015 · 7 comments

Comments

@elincognito
Copy link

I have the following html
<input id="passwordConfirm" name="passwordConfirm" type="password" data-ng-model="user.confirmPassword" data-match="user.password" placeholder="{{'password'|i18n}}" class="form-control input-md" required>

in the controller i want to reset the value of the field so
i tried

$scope.user.confirmPassword = ''
and
$scope.user.password = ''
Nothing happens however the expected result would be to the input be cleared

When i remove the data-match directive everything works as expected

@TheSharpieOne
Copy link
Owner

Yeah, so it appears that based on everything else that is going on in this directive that when the internal $modelValue is falsey it will not update the view.

Work-around, hopefully a temporary work-around (i'll see what I can do to fix this when I have time) is to manually set the view value using formName.fieldName.$setViewValue("") and then rendering it with formName.fieldName.$render()

$scope.user.confirmPassword = "";
form.confirmPassword.$setViewValue("");
form.confirmPassword.$render();

Also, which version of angular are you running and which version of this script are you running? I think this probably only exists in the 1.2.x branch, the 1.3.x branch shouldn't suffer this problem.

@elincognito
Copy link
Author

im using 1.2.28

@ragefuljoe
Copy link

I'm using 1.4.1 and running into the same problem. Blanking out both fields still shows error.

@TheSharpieOne
Copy link
Owner

But what version of angular?

@ragefuljoe
Copy link

angular 1.3.15

@romelgomez
Copy link

Hi friends, this is not a bug,

I have written this example to prove it:

https://jsfiddle.net/venezuela/wqrxfbrz/

In summary:

var original = angular.copy($scope.model = {
  'email':'',
  'password':''
});

$scope.resetForm = function(){
  angular.copy(original,$scope.model);
  $scope.formName.$setUntouched();
  $scope.formName.$setPristine();
};

Plus: if you have a array $scope.files = [], the angular way to reset a array is using $filter or angular.copy:

$scope.removeInvalidFiles = function(){
  $scope.model.files = $filter('filter')($scope.model.files, function(value) { return !angular.isDefined(value.$error);});
};

Other example: https://jsfiddle.net/venezuela/atpaz7w7/

Also see this stackoverflow questions:
http://stackoverflow.com/questions/13085024/reset-a-model-with-angular-js?lq=1
http://stackoverflow.com/questions/17559258/angularjs-reset-scope-data-to-original-values?lq=1

@ticklemepierce
Copy link

Hey, just chiming in to let everyone know that I figured out the issue.

<input id="passwordConfirm" name="passwordConfirm" type="password" data-ng-model="user.confirmPassword" data-match="user.password" placeholder="{{'password'|i18n}}" class="form-control input-md" required>

This is comparing against user.password. When you go to an empty field in the password field, user.password = undefined. This is comparing against the Confirm Field string. undefined === "" is false.

I just changed mine to compare against the Password Form field instead of the data in scope and then it is comparing an empty string to empty string and then (since "" === "" is true) the error doesn't show in this case.

Hope this helps some people!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants