How to display server errors in Angularjs with ng-messages

后端 未结 4 1881
情话喂你
情话喂你 2021-02-02 10:30

I have have my angular app validating a sign-up form. On submit, the server also validates the data. I\'m outputting error messages in angular using ng-messages.

Here i

4条回答
  •  日久生厌
    2021-02-02 10:56

    First of all you should set validity and error messages

          $scope.formErrors = {};
          angular.forEach(errors, function(data, name) {
            if (!vm.register_form[name]) {
              return;
            }
            $scope.formErrors[name] = data.message;
            //this will set errors->server to invalid state
            $scope.register_form[name].$setValidity('server', false);
          });
    

    The next step will be rendering by ng-messages

          
    Email is required
    Invalid email
    {{formErrors.email}}

提交回复
热议问题