如果ng-repeat存在一些错误,则禁用设置按钮

时间:2014-10-15 18:42:03

标签: angularjs

我使用ng-repeat生成一些行,我根据特定条件显示每行的错误。我的代码工作得很好。现在,如果我有任何错误,我想禁用一个超出ng-repeat范围的按钮。我怎么能这样做?

html代码

<div ng-repeat="item in itemList">
    <input type="text" ng-model="item.name" />
    <input type="text" ng-model="item.phone" />
    <div ng-if="validateRow(item)">Here is my error</div>
</div>
<button ng-disabled="I want to disabl this if there is some error for the above ng-repeat">Click </button>

js code

$scope.validateRow = function(item) {
    return true or false based on item values
}

2 个答案:

答案 0 :(得分:0)

这个例子应该足够了

 <div ng-repeat="item in itemList">
<input type="text" ng-model="item.name" />
<input type="text" ng-model="item.phone" />
<div ng-show="validateRow(item)">Here is my error</div>
</div>
<button ng-disabled="isDisabled">Click </button>



function MyCtrl($scope) {
$scope.itemList = [{"name": "", "phone": "123"},{"name": "", "phone": "456"},{"name": "", "phone": "789"}];

$scope.disable= false;

$scope.validateRow = function(item) {

$scope.disable= false;
    for(var c =0; c < $scope.itemList.length; c++){

        if($scope.itemList[c].name != "abc"){

            $scope.disable = true;
            break;
        }
    }

   return item.name != 'abc';
}

}

使用链接工作进行更新

http://jsfiddle.net/LXAt7/125/

答案 1 :(得分:0)

在视图中

<button ng-disabled="disable">Click </button>

在控制器中,

$scope.disable= false;
$scope.validateRow = function(item) {
   if(true based on item values) // if your going to return true
      $scope.disable= true;
   }
   return true or false based on item values
}