2016-08-23 175 views
1

我需要在提交后获取服务器响应错误消息以在窗体中显示。我得到的消息,如果我在JS服务器响应消息

.error(function (data, status, header, config) { 
    $scope.postDataSuccessfully = false; 
    var messages = []; 
    angular.forEach(data.errors, function (value, key) { 
     $scope.ResponseDetails = "Data: " + data + 
       "<hr />status: " + status + 
       "<hr />headers: " + header + 
       "<hr />config: " + config; 
     messages.push('Error! ' + key + ' is not correct'); 

    }); 
    $scope.messages = messages; 
}); 

,并在HTML

<div ng-repeat="message in messages track by $index"> 
    <div data-ng-hide="message == ''" data-ng-class="(postDataSuccessfully) ? 'alert alert-success' : 'alert alert-danger'"> 
     {{message}} 
    </div> 
</div> 

使用此代码,但这只是显示在一个地方后提交,这样的形象 enter image description here

我要上有错误的字段上的消息,如下图 enter image description here

这是我的html代码这种形式的输入

<div class="form-group"> 
    <label for="type" class="col-sm-3 control-label">{{'TYPE'| translate}}</label> 
    <div class="col-sm-9"> 
    <select class="form-control" id="type" name="type" ng-model="type"> 
     <option ng-repeat="type in types" value="{{type}}">{{type}}</option> 
    </select> 


    </div> 

</div> 
<div class="form-group"> 
    <label for="description" class="col-sm-3 control-label">{{'DESCRIPTION'| translate}}</label> 
    <div class="col-sm-9"> 
    <textarea type="text" rows="3" class="form-control" id="description" name="description" ng-model="description" placeholder="description"></textarea> 
    <br> 
    </div> 
</div> 

回答

2

有两个选项: 要么你把每个字段的错误消息,并且当它发生

<div ng-messages="myForm.myselect.$error" ng-if="myForm.$submitted || myForm.myselect.$touched"> 
<div ng-message="required">myselect is required.</div> 
</div> 

显示它或者可替换地可以使用吐司弹出错误封邮件:

$scope.validateMyData = function() { 
     var errorType = callYourServerService(); 

    if(errorType === "myFirstField") { 
     toaster.pop('error', "Error", "Please insert valid value for myFirstField"); 
    } 
    if(errorType === "mySecondField") { 
     toaster.pop('error', "Error", "Please insert valid value for mySecondField"); 
    } 

...等

+0

嗨,thnx回应。我尝试这个,但不工作,我尝试用烤面包机。添加了脚本和CSS。调用submit validateMyData(),并添加所有errorType,但有任何通知。采用了棱角分明材质 : – Arter

+0

你可以按照演示之一 https://material.angularjs.org/latest/demo/toast .... 或者用角与Bootsrap https://github.com/ jirikavi/AngularJS,烤面包机 希望这有助于 问候 – xelilof

+0

日Thnx,我会尽量 – Arter