Angularjs 尝试触发更改时出错

Angularjs 尝试触发更改时出错,angularjs,validation,Angularjs,Validation,我有两个文本框的形式,它完全相同,因为它是一个指令,通过ng重复与隔离范围呈现 <form name="inputForm" class="input-icon right" ng-class="{'has-error': hasError()}"> <input type="text" ng-model="model" class="form-control input-medium" name="textbox

我有两个文本框的形式,它完全相同,因为它是一个指令,通过ng重复与隔离范围呈现

<form name="inputForm" class="input-icon right"
      ng-class="{'has-error': hasError()}">
    <input type="text"
           ng-model="model"
           class="form-control input-medium" name="textbox" ng-required="isRequiredValidation()">
    <ul class="error-message" ng-show="hasError()">
        <li ng-repeat="error in showErrors">{{error.message}}</li>
    </ul>
</form>
<form name="inputForm" class="input-icon right"
      ng-class="{'has-error': hasError()}">
    <input type="text"
           ng-model="model"
           class="form-control input-medium" name="textbox" ng-required="isRequiredValidation()">
    <ul class="error-message" ng-show="hasError()">
        <li ng-repeat="error in showErrors">{{error.message}}</li>
    </ul>
</form>
但我有一个错误:

Error: [$rootScope:inprog] $apply already in progress
http://errors.angularjs.org/1.2.16/$rootScope/inprog?p0=%24apply
    at http://localhost:29549/Scripts/angular-1.2.16/angular.js:78:12
    at beginPhase (http://localhost:29549/Scripts/angular-1.2.16/angular.js:12720:15)
    at Scope.$apply (http://localhost:29549/Scripts/angular-1.2.16/angular.js:12509:11)
    at HTMLInputElement.listener (http://localhost:29549/Scripts/angular-1.2.16/angular.js:16632:15)
    at HTMLInputElement.jQuery.event.dispatch (http://localhost:29549/Scripts/jquery-1.10.2.js:5109:9)
    at HTMLInputElement.elemData.handle (http://localhost:29549/Scripts/jquery-1.10.2.js:4780:46)
    at Object.jQuery.event.trigger (http://localhost:29549/Scripts/jquery-1.10.2.js:5021:12)
    at HTMLInputElement.<anonymous> (http://localhost:29549/Scripts/jquery-1.10.2.js:5705:17)
    at Function.jQuery.extend.each (http://localhost:29549/Scripts/jquery-1.10.2.js:671:23)
    at jQuery.fn.jQuery.each (http://localhost:29549/Scripts/jquery-1.10.2.js:280:17)  
错误:[$rootScope:inprog]$apply已在进行中
http://errors.angularjs.org/1.2.16/$rootScope/inprog?p0=%24apply
在http://localhost:29549/Scripts/angular-1.2.16/angular.js:78:12
初期(http://localhost:29549/Scripts/angular-1.2.16/angular.js:12720:15)
在范围内。$apply(http://localhost:29549/Scripts/angular-1.2.16/angular.js:12509:11)
在HTMLInputElement.listener(http://localhost:29549/Scripts/angular-1.2.16/angular.js:16632:15)
在HTMLInputElement.jQuery.event.dispatch(http://localhost:29549/Scripts/jquery-1.10.2.js:5109:9)
在HTMLInputElement.elemData.handle(http://localhost:29549/Scripts/jquery-1.10.2.js:4780:46)
在Object.jQuery.event.trigger处(http://localhost:29549/Scripts/jquery-1.10.2.js:5021:12)
在HTMLInputElement。(http://localhost:29549/Scripts/jquery-1.10.2.js:5705:17)
在Function.jQuery.extend.each(http://localhost:29549/Scripts/jquery-1.10.2.js:671:23)
在jQuery.fn.jQuery.each(http://localhost:29549/Scripts/jquery-1.10.2.js:280:17)
第一个文本框触发change ok,但第二个文本框出现此错误。 请帮我把这个修好。
我的主要目的是从表单外部触发文本框的验证。

看起来jQuery
trigger()
导致循环中的每个元素调用
$apply()

它对导致错误的
$apply
的多次调用。FWIW,混合使用jquery和angular很少是个好主意,如果需要jquery等价的操作,通常可以使用jqLite

编辑

我找到了一些类似的角度代码。forEach的
forEach
可能更接近您的目标。如果字段是原始的,则会触发“必填字段”错误消息。它不需要任何手动调用
apply()

$scope.submit = (form) ->
    if form.$valid
        $modalInstance.close($scope.data)
    else
        angular.forEach form, (field) ->
            field.$setViewValue(field.$viewValue) if field.$pristine

在我看来,这似乎与angular的公开问题有关:

要解决此问题,您可以将函数包装在不调用脏检查(最后一个参数为false)的$timeout中,或者使用安全应用:

function safeApply(scope, fn) {
    (scope.$$phase || scope.$root.$$phase) ? fn() : scope.$apply(fn);
}

您在isRequiredValidation()中做什么?您遇到的错误通常是由于不必要地调用了作用域。$apply(),或者调用得太早。@comradburk:此函数验证此控件是否需要验证?如果为true,则需要的属性将添加到此文本框。当我使用angular jquery lite
angular.element('form input')
function safeApply(scope, fn) {
    (scope.$$phase || scope.$root.$$phase) ? fn() : scope.$apply(fn);
}