ng-repeat部分

时间:2017-01-23 10:29:31

标签: angularjs angularjs-ng-repeat

我在AngularJs中添加了一个ng-repeat部分。我已经添加了一个必需的字段验证器。但是,当清空所有字段时,页面高度会增加,因为span标记数据会显示出来。在描述错误的ng-repeat部分之后,是否可以只显示一条错误消息。当前的UI代码如下:

<div class="row" ng-if="selectedSeasonType">
                    <div class="form-group ">
                        <label class="form-group col-md-3">Language</label>
                        <label class="form-group col-md-4">Title</label>
                        <label class="form-group col-md-5">Description</label>
                    </div>
                </div>
                <br />



          <div class="row" ng-repeat="Descriptions in seasonsWithDescription" ng-if="selectedSeasonType">
                    <div class="form-group col-md-2 top-Margin-language">
                        <label ng-model="Descriptions.Language">{{Descriptions.Language}}</label>
                    </div>
                    <div class="form-group col-md-4 top-Margin-Title">
                        <input type="text" maxlength="150" class="form-control input-md" required="" name="titleValidate_{{$index}}" ng-model="Descriptions.Title" />
                        <span style="color:red" ng-show="submitted == true && mainForm.titleValidate_{{$index}}.$error.required">Title is required</span>
                    </div>
                    <div class="form-group col-md-5">
                        <textarea maxlength="500" class="form-control input-md noresize" required="" name="descriptionValidate_{{$index}}" noresize="" ng-model="Descriptions.Description"></textarea>
                        <span style="color:red" ng-show="submitted == true && mainForm.descriptionValidate_{{$index}}.$error.required">Description is required</span>
                    </div>
                    <div class="form-group col-md-1">
                        <a style="cursor:pointer">
                            <img ng-src="{{DeleteIcon_url}}" alt="delete image" ng-click="($index == !selectedDeleteIcon) || seasonsWithDescription.splice($index,1)" ng-class="{'disabled': $first}" />
                        </a>
                    </div>
                </div>

如何在ng-repeat部分之后仅添加一条错误消息来描述AngularJs中所需的数据? 感谢

1 个答案:

答案 0 :(得分:0)

根据评论,这里有plnkr以满足您的需求。

 $scope.isTitleMissing = false;

$scope.cars = [{id: 1, 'brand': 'Chevy', 'model': 'Camarro'},
{id: 1, 'brand': 'Ford', 'model': 'Mustang'},
{id: 1, 'brand': 'Hyundai', 'model': 'Santa Fe'},
{id: 1, 'brand': 'Honda', 'model': 'CRV'},
{id: 1, 'brand': 'BMW', 'model': '350'}]


$scope.submitForm = function(){

 var ctr = $scope.cars.length;
 var titleCtr = 0;
 for(var i = 0 ; i < ctr ; i++){
   if($scope.cars[i].title != null){
     titleCtr++;
   }
 }
//

if(titleCtr < ctr){
  $scope.isTitleMissing = true;
}else{
  //continue with submit
   $scope.isTitleMissing = false;
  // alert('Total cars - ' + ctr + ' and total Title '+ titleCtr);
}

它只包含总记录数,另一个记录总计瓷砖数。如果后者小于总记录数,则显示错误消息,否则继续提交。

希望这有帮助。

相关问题