How to validate table row data with Angular?

后端 未结 4 1789
醉梦人生
醉梦人生 2021-02-09 16:42

I have table with ng-repeat for table rows. I want to make inline editing and validation of data from single row, but it is impossible to use for

4条回答
  •  遇见更好的自我
    2021-02-09 17:06

    I create example with simple validation to your second question:

    View:

    Id Firstname Lastname
    {{item.id}}

    Controller:

    function MyCtrl($scope) {
        $scope.items = [
            {
                id: 1,
                firstName: 'Ivan',
                lastName: 'Ivanov',
                email: 'email@email.com'
            },
            {
                id: 2,
                firstName: 'Petr',
                lastName: 'Petrov',
                email: 'email@email.com'
            }
        ];
    
        $scope.emailRegExp = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/; 
    }
    

    Please, see jsfiddle :)

    Jsfiddle

    For your question in comment about date validation:

    I see two ways for do it:

    1) In yor controller you create $scope.dateRegExp = "/^\d{2}([./-])\d{2}\1\d{4}$/" and into the view you using it with ng-pattern="dateRegExp"

    2) You can use ng-change="" directive:

    View:

    
        
    

    Controller:

    $scope.dateInputError = false;
    
    $scope.validateDate = function(date) {
       if(//some validation){
          $scope.dateInputError = true; //true - it means error style shows
       }
    };
    

提交回复
热议问题