我正在从服务进行$ http调用,并使用$ httpBackend使用json数据进行响应。最初和下面的加载失败是我在控制台中收到的错误消息。我刷新页面几次后就可以了。
SyntaxError: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead jquery.min.js:1
Error: http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js is being assigned a //# sourceMappingURL, but already has one
"Error: Unexpected request: GET lookup/program-categories
No more request expected
$httpBackend@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular-mocks/angular-mocks.js:1179
sendReq@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:8181
$http/serverRequest@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:7921
qFactory/defer/deferred.promise.then/wrappedCallback@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:11319
qFactory/defer/deferred.promise.then/wrappedCallback@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:11319
qFactory/ref/<.then/<@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:11405
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:12412
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:12224
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:12516
done@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:8204
completeRequest@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:8412
createHttpBackend/</xhr.onreadystatechange@http://<hostname>.amazonaws.com/ostnfe/app/vendor/angular/angular.js:8355
"
以下是代码。服务'lookups'进行http调用,控制器'SearchCtrl'将值添加到html范围。
var stubbedOstnApp = angular.module('stubbedOstnApp', [
'ostnApp',
'ngMockE2E'
]);
stubbedOstnApp.run(['$httpBackend','$http',function($httpBackend, $http){
$http.get('../test/data/program-categories.json').then(function(data){
$httpBackend.whenGET('lookup/program-categories').respond(data.data)
});
$httpBackend.whenGET(/^views\//).passThrough();
$httpBackend.whenGET(/^\.\.\/test\/data\//).passThrough();
}]);
var ostnApp = angular.module('ostnApp', [
'ngRoute',
'ostnControllers'
]);
ostnApp.service('lookups',['$http',function($http){
var programCategories = {};
$http.get('lookup/program-categories').then(function(data){
for(var i=0;i<data.data.length;i++){
var programCategory = data.data[i];
programCategory.selected = false;
programCategories[programCategory.id] = programCategory;
}
});
this.programCategories = function(){
return programCategories;
}
}]);
ostnControllers.controller('SearchCtrl',['$scope', '$http', 'lookups', function($scope, $http, lookups){
//this gets displayed in the html
$scope.programCategories = lookups.programCategories();
}]);
答案 0 :(得分:0)
因此,如果在promise之外移动模拟使其以null
响应,则意味着在发送请求时不会初始化模拟。尝试在运行应用程序之前预先加载它,以便不等待响应并立即进行模拟。
答案 1 :(得分:0)
它似乎试图在没有完全加载的情况下访问数据。 在ngRoute下使用&#39;解决&#39;在加载Controller之前调用您的服务的属性。