admin管理员组

文章数量:1346672

i have code like this, where i set options {allowInvalid: true}

<input type="number" name="userName"
         ng-model="user.name"
         ng-model-options="{ allowInvalid: true }"
         ng-blur="test(user)"/>

When i input non number value into non number, example: abc,
why user.name still changed to undefined?
where i expected the model user.name should be 'abc'.

Full code: i'm using angular v1.3.16

i have code like this, where i set options {allowInvalid: true}

<input type="number" name="userName"
         ng-model="user.name"
         ng-model-options="{ allowInvalid: true }"
         ng-blur="test(user)"/>

When i input non number value into non number, example: abc,
why user.name still changed to undefined?
where i expected the model user.name should be 'abc'.

Full code: http://plnkr.co/edit/BsLk2GWVkchNgKaOV453?p=preview i'm using angular v1.3.16

Share Improve this question asked Jun 6, 2015 at 13:53 GusDeCooLGusDeCooL 5,76118 gold badges70 silver badges103 bronze badges 6
  • 1 because you use type="number" in this case for invalid input - value is undefined – Grundy Commented Jun 6, 2015 at 13:57
  • 1 Following the docs.angularjs/api/ng/input/input%5Bnumber%5D, you can't use allowInvalid with type="number" attribute – Kosta Commented Jun 6, 2015 at 14:05
  • @grundy that were the purpose. i use allowInvalid: true. So if i input invalid value. it's should not change value to undefined :( – GusDeCooL Commented Jun 6, 2015 at 14:06
  • 1 see more in @Kosta ment :-) – Grundy Commented Jun 6, 2015 at 14:07
  • @Kosta thanks man. you have answered the question. can you please put that as answer so i can accept it. thanks :) – GusDeCooL Commented Jun 6, 2015 at 14:11
 |  Show 1 more ment

1 Answer 1

Reset to default 11

Following the link, you can't use allowInvalid with type="number" attribute.

Issues with HTML5 constraint validation

In browsers that follow the HTML5 specification, input[number] does not work as expected with ngModelOptions.allowInvalid. If a non-number is entered in the input, the browser will report the value as an empty string, which means the view / model values in ngModel and subsequently the scope value will also be an empty string.

本文标签: javascriptAngular ngModelOptions allowInvalid not workingStack Overflow