我正在尝试使用虚拟服务来获取Angular中的数据,我正在使用ngMockE2E,而我的Mock代码如下所示:
(function () { "use strict" var app = angular .module("productResourceMock",["ngMockE2E"]); app.run(function ($httpBackend) { var products = [ { "productId": 1,"productName": "mobile1","productCode": "heh4","releaseDate": "May 21,2013","description": "very nice mobile","cost": 200,"price": 300,"category": "Electronics","tags": ["mobile","electronic"],"imageUrl": "images/img1.jpg" },{ "productId": 2,"productName": "mobile2",2012","description": "not a nice mobile","cost": 100,"price": 500,"Electronic"],"imageUrl": "images/img2.jpg" }]; var productUrl = "/api/products"; $httpBackend.whenGet(productUrl).respond(products); }); }());
我已经定义了我的控制器,在里面,它有这个代码:
(function () { "use strict" angular .module("productManagement") .controller("ProductListCtrl",["productResource",ProductListCtrl]); function ProductListCtrl(productResource) { var vm = this; productResource.query(function(data){ vm.products = data; }); } }());
对于我发送REST请求的服务,我有这样的代码:
(function () { "use strict" angular .module("common.services") .factory("productResource",["$resource",productResource]); function productResource($resource) { return $resource("/api/products/:productId"); } }());
我仍然收到此错误:未捕获TypeError:$httpBackend.whenGet不是一个函数.
如有任何帮助,或需要任何澄清,请告诉我.
答案很简单:用whenGET时取代
原文链接:https://www.f2er.com/angularjs/141222.html小心将http动词全部写成大写.