使用$ setPristine()的ng-form

时间:2015-08-13 09:56:56

标签: javascript angularjs angularjs-ng-form

<tr ng-form="fbForm_{{$index}}">
    <td><center><p ng-hide="fb.editMode">{{fb.clientId}}</p>
        <input type="text" class="form-control" name="clientId" ng-show="fb.editMode" required
               ng-model="fb.clientId"/></center>
        <span ng-show="fbForm_{{$index}}.clientId.$dirty && fbForm_{{$index}}.clientId.$error.required">Client Id is required.</span>
    </td>
    <td><center><p ng-hide="fb.editMode">{{fb.clientSecret}}</p>
        <input type="text" class="form-control" name="clientSecret" ng-show="fb.editMode" required
               ng-model="fb.clientSecret"/></center>
        <span ng-show="fbForm_{{$index}}.clientSecret.$dirty && fbForm_{{$index}}.clientSecret.$error.required">Client secret is required.</span>
    </td>
    <td style="width:10%">
        <p ng-hide="fb.editMode"><a ng-click="toggleEdit()" href="javascript:;">Edit</a> | <a ng-click="deletefacebook()" href="javascript:;">Delete</a></p>
        <p ng-show="fb.editMode"><a class="btn btn-primary simple_button"  ng-disabled="fbForm_{{$index}}.$pristine || fbForm_{{$index}}.$invalid" data-ng-click="save($index); fbForm_{{$index}}.$setPristine()" href="javascript:;">Save</a> | <a class="btn btn-primary simple_button" ng-click="cancel($index,fb.fbConfigId)" href="javascript:;">Cancel</a></p>
    </td>
</tr>

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

对于ng-disabled我可以使用{{$ index}} 在pristine函数表单名称中获取语法错误。

1 个答案:

答案 0 :(得分:1)

ngDisabledngClick指令期望表达式,所以它应该是这样的:

ng-disabled="this['fbForm_' + $index].$pristine || this['fbForm_' + $index].$invalid" 
ng-click="save($index); this['fbForm_' + $index].$setPristine()"