How ng-message is connected to model, and how can I show the message using ng-message?

前端 未结 3 1240
盖世英雄少女心
盖世英雄少女心 2021-01-01 19:13

In this plunk the objective is to show an error message based on validation in the controller (instead of the built-ins required or min-length). Th

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 19:24

    The Dmitry K's answer it's excellent.

    I´m going to expand the answer.

    //Function before show your form:
    vm.showForm(form){
            form.$setPristine();
            form.$setUntouched();
            form.myFieldName.$setValidity('myCustomValidationName', false);
    
            //More code...
    }
    
    //funtion to validate field on "ng-change"
    vm.validateField(form){
    
        if(xxxx == yyy) //Make your own validation{
           form.myFieldName.$setValidity('myCustomValidationName', true);
        }else{
           form.myFieldName.$setValidity('myCustomValidationName', false);
        }
    
    }
    

    And the relevant HTML code:

    this is the message to show

提交回复
热议问题