getting Error: [$rootScope:inprog] $digest already in progress when changed from Fetch to $http + $q

getting Error: [$rootScope:inprog] $digest already in progress when changed from Fetch to $http + $q

我正在尝试在 Angular.js 工厂内执行 http 请求。最初我使用 Fetch API 并且控制台没有显示任何错误。我使用 $http$q 实现了它,现在它在我的控制台上显示错误,即使该功能似乎有效。

我得到

"Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.0/$rootScope/inprog?p0=%24digest
    at https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:68:12
    at beginPhase (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:16073:15)
    at Scope.$apply (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:15817:11)
    at n.<anonymous> (https://cdpn.io/cp/internal/boomboom/pen.js?key=pen.js-d35c4e85-c842-3510-cf4e-9d0946fca47f:96:16)
    at n.emit (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:1:2344)
    at https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:4:24433
    at n (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:1:8521)
    at n (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:2:26916)
    at n._dispatchAlgoliaResponse (https://cdnjs.cloudflare.com/ajax/libs/algoliasearch-helper-js/2.28.1/algoliasearch.helper.min.js:4:24330)
    at processQueue (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular.js:14454:28)"

我是 Angular.js 的新手,不确定发生了什么。
我实现的codepen https://codepen.io/cmgchess/pen/GRMLLwV

我的工厂

var index = 'bestbuy';
var alSH = angular.module('AlgoliaSearchHelper', ['ngSanitize']);

// Expose the helper
alSH.factory('helper',helper);

helper.$inject = ['$http', '$rootScope', '$q'];

function helper($http, $rootScope, $q) {

  
  
  var customSearchClient = {
    search(requests) {
      var deferred = $q.defer();
      $http.post('https://algolia-backend-search.herokuapp.com/search',{requests}).success(function (response) {
        deferred.resolve(response);

      });
      return deferred.promise;
    }
    // search(requests, cb) {
    //   return fetch('https://algolia-backend-search.herokuapp.com/search', {
    //     method: 'post',
    //     headers: {
    //       'Content-Type': 'application/json',
    //     },
    //     body: JSON.stringify({ requests }),
    //   }).then(function(res){return res.json()}).then(cb)
    // }
  };
  
  
  return algoliasearchHelper(customSearchClient, index, {
    disjunctiveFacets: ['category'],
    hitsPerPage: 7,
    maxValuesPerFacet: 3
  });
};

我在 https://codepen.io/cmgchess/pen/GRMLLwV

的 3 个不同地方使用了 $scope.$apply
    helper.on('result', results => {
         $scope.$apply($scope.something = results.something );
    });

我看到了这篇文章 https://medium.com/@TuiZ/digest-already-in-progress-c0f97a6becfc,它说

  1. In a promise Using $q, the Angular’s promise library, you are sure to be in the Angular context, so no need to $apply or $digest
    Angular create a new $digest only cycle, when the promise is resolved, so if you call $scope.$digest() or $apply() inside the promise resolve callback, you will get “$digest already in progress”

所以我所做的就是改变我用过的所有地方 $scope.$apply 这样

    helper.on('result', results => {
         $scope.something = results.something;
    });

现在我没有收到错误。我不确定我是否理解得很好,所以如果有人给出更好的答案,我将不胜感激。