【问题标题】:Angularjs error in call $scope.$apply()调用 $scope.$apply() 中的 Angularjs 错误
【发布时间】:2015-12-14 02:44:02
【问题描述】:

调用$digest或apply时出错

  registerAjax = ->
    successRegister = ->
      $scope.showRegistrationPanel = false
      $scope.showEndRegistrationPanel = true
      $scope.$digest()
   errorRegister = (response) ->
      $scope.textErrorPanelRegistration = response.data.error
      $scope.showErrorPanel = true
      $scope.$apply()



   data =
     password: $scope.user.password
     firstName: $scope.user.firstName
    lastName: $scope.user.lastName
    email: $scope.user.email

   http.post REGISTER_URL, data
    .then successRegister, errorRegister

当调用 $digest 或 $apply 时错误消失,但一切正常

Error: [$rootScope:inprog] http://errors.angularjs.org/1.4.7/$rootScope/inprog?p0=%24digest
at Error (native)
at http://localhost:8000/lib/angular/angular.min.js?M420gm3LwzcQLAcaXrk6IQ:6:416

【问题讨论】:

  • 您是否在代码中使用jQuery.ajax..那么显然您需要运行摘要循环..或者您可以尝试$timeout(function(){}) 运行安全摘要循环
  • 所以没有必要运行摘要循环..如果您使用$http方法制作ajax。

标签: javascript angularjs coffeescript


【解决方案1】:

使用 Angular 的$timeout service:

$timeout(function() {
  $scope.$apply();
});

这将在调用 $apply 之前自动等待当前摘要周期完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多