How can I override Angular's filtering of invalid form values, forcing Angular to persist the $viewValue to $modelValue?

前端 未结 4 2123
终归单人心
终归单人心 2020-12-31 12:54

I need to be able to temporarily persist data that isn\'t fully validated yet, then enforce validation when I\'m ready to make it permanent. But Angular is preventing that.

4条回答
  •  耶瑟儿~
    2020-12-31 13:55

    The following solution can be used since Angular version 1.3:

    You can set ng-model-options="{ allowInvalid: true }" in the input field of the model where you want to persist invalid attributes.

    allowInvalid: boolean value which indicates that the model can be set with values that did not validate correctly instead of the default behavior of setting the model to undefined

    https://docs.angularjs.org/api/ng/directive/ngModelOptions

    Then, when you are ready to show the user their validation errors, you are free to do it your way. Just remember to give your inputs and forms name attributes, so that you can reference them in your scope.

    E.g. if($scope.myFormName.my_input_name.$invalid) { ... }

    A relevant tutorial: http://blog.thoughtram.io/angularjs/2014/10/19/exploring-angular-1.3-ng-model-options.html

提交回复
热议问题