Don't record invalid values with ng-model

前端 未结 3 1799
陌清茗
陌清茗 2020-12-18 00:08

I really like how the ng-model attribute binds directly to my model and users get instant feedback on their changes. For my use case that\'s perfect. However, I don\'t wan

3条回答
  •  眼角桃花
    2020-12-18 00:39

    As Chandermani said, it is the default behavior, here is a example that shows it in action :

    Is the input valid ? {{ myform.myinput.$valid }}
    Input's value : {{ myvalue }}

    {{ myvalue }} doesn't show anything until you write at least 4 characters in the input.

    Best Regards.

    EDIT

    If you need a default value, I guess you could break down your value into 2 values, using a computed value :

       var validNumber = 15;
        $scope.validNumber = function () {
            if ($scope.myform.myNumber.$valid) return $scope.myNumber;
            else return validNumber;
        };
    

    I set up an example here : http://jsfiddle.net/7vtew/1/

提交回复
热议问题