调用$scope时发生Angularjs错误$apply()

Angularjs error in call $scope.$apply()

本文关键字:错误 apply Angularjs scope 调用      更新时间:2023-09-26

调用$digest或应用时发生错误

  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

使用Angular的$timeout服务:

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

这将自动等待当前摘要周期完成,然后再调用$apply。