Angular ng messages show errors on ng repeat form

后端 未结 3 1536
醉酒成梦
醉酒成梦 2021-02-19 22:49

I want to generate inputs with ng repeat, the problem is when i have an error this is only apply on the last element, how can i make this apply per element?

<         


        
3条回答
  •  别跟我提以往
    2021-02-19 23:30

    The ngMessage is like a switch case you need to pass the correct group to detect the error, when you create a form in angular he wraps the form by the name of the form and the name of your inputs for example:

    Wrap to something like this with there repective $error each:

    myform = {
     myInput: {
      $error:{
       //...
      }
     },
     myInput2: {
       //...
     },
     myInput3:{
       //...
     },
       //...
    }
    

    You are using the name of the form, and not contain the real error of each element (contains an array with all relevant data of each error), you must usage the name of input names like this plunkr:

    http://plnkr.co/edit/I43HTQeWZS85N55hXGfF?p=preview

    HTML:

    This field is required

    JS:

    var app = angular.module('plunker', ['ngMessages']);
    
    app.controller('MainCtrl', function($scope) {
    
      $scope.players = [{
        name: "jhon",
        id:1 
      },
      {
        name: "jhon1",
        id:2 
      },
      {
        name: "jhon2",
        id:3 
      },
      {
        name: "jhon3",
        id:4 
      }
        ];
    });
    

提交回复
热议问题