【发布时间】:2022-01-19 17:20:58
【问题描述】:
我正在尝试在 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 的新手,不知道发生了什么。
我实现的代码笔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
});
};
【问题讨论】:
标签: angularjs algolia angularjs-http angularjs-digest angularjs-q