当前位置: 首页 > 知识库问答 >
问题:

在ng repeat段后显示错误消息

曾景龙
2023-03-14

我在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个答案

冯良才
2023-03-14

根据评论,这里有一个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);
}

它只是有一个总记录数,另一个是总分片数。如果后者小于总记录数,则显示错误消息,否则继续提交。

希望这有帮助。

 类似资料:
  • 问题内容: 我安装了XAMPP 1.7.4(使用PHP 5.3.5),问题是PHP不显示任何错误消息。例如,如果我使用不带参数的MYSQL连接,PHP将不会抱怨必填字段。 为什么是这样? 如何配置PHP以显示错误? 问题答案: 要在脚本级别启用错误,请在脚本顶部添加: 或者,如果它 不是 生产站点,而仅仅是开发/测试站点,则可以在php.ini中打开错误报告。搜索以下设置:

  • 我正在尝试用spring显示一个报告PDF,我终于得到了它,但是问题出现在我的代码中有一个异常或错误的时候。在这种情况下,我想在我的应用程序中显示特殊的消息。sping控制器返回一个带有文件pdf的ResponseEntity和一个http状态代码。

  • 为我的JavaFX项目安装了scene builder,安装完成后显示以下错误:, 场景构建器的版本:场景Builder-8.3.0.exe从http://gluonhq.com/labs/scene-builder/下载 我试图集成与Eclipse霓虹灯4.6和Java1.8.0版本与win 7专业64位操作系统 此外,我还尝试从windows提供exe路径--

  • 我已经找到了一些答案,但我的问题仍然没有解决。 我刚刚制作了一个新的应用程序,发现注册页面上的错误消息根本没有显示出来。以下是我在本网站上找到的一些答案: 只需删除,'中间件'= 或 \照明\会话\中间件\开始会话::class,\照明\视图\中间件\ShareErrs From会话::class, 从受保护的$middlewareGroup到karnel中受保护的$middleware中间件。p

  • 我在bean每个字段上都有多个验证注释,但我希望在使用JSR303的spring MVC应用程序中只显示每个字段的第一个失败消息。目前,它正在评估所有约束并显示每个字段的所有消息。当一个文件为空时,我不想计算它的最小和最大约束。 我尝试过使用组,但它并不像预期的那样工作,而且我需要显式地调用验证器,比如validator.validate(),这是我想避免的。 是否有任何替代方法可以通过过度编写一