1

我有一个相当简单的控制器,它获取一个简单的 json 对象列表......

function ProductGroupsCtrl($scope, $http, $routeParams, sharedService, popupService) {

$scope.list = null;
$scope.selectedItem = null;
$scope.selectedItemJsonString = '';

$scope.selectItem = function (item) {
    $scope.selectedItem = item;
    $scope.selectedItemJsonString = JSON.stringify(item);
    //alert(JSON.stringify(item));
};

$scope.closePopup = function () {
    $scope.selectedItem = null;
    $scope.selectedItemJsonString = '';
};

// sharedService.prepForBroadcast($routeParams.anotherVar);

$http({
    method: 'GET',
    url: '/ProductGroup'
}).success(function (data) {
    $scope.list = data;
}).
error(function (data) {
    $scope.message = 'There was an error with the data request.';
});

}

然后我尝试在测试类中模拟请求:

var scope, ctrl, $httpBackend, sharedServiceMock = {}, popupServiceMock = {};

beforeEach(inject(function (_$httpBackend_, $rootScope, $controller) {
    $httpBackend = _$httpBackend_;

    $httsypBackend.expectGET('/ProductGroup').
    respond([{
        ProductGroupID: 5,
        MenuTitle: "Promotional Products",
        AlternativeText: "Coming soon - a collection of environmentally friendly Promotional Products",
        OrdinalPosition: 5,
        Active: false
    }]);


    scope = $rootScope.$new();
    ctrl = $controller(ProductGroupsCtrl, {
        $scope: scope,
        $http: $httpBackend,
        sharedService: sharedServiceMock,
        popupService: popupServiceMock
    });}));

但是我在睾丸窗口中收到一个错误object undefined。我在这里做错了什么?

4

1 回答 1

1

找到了答案。如果我从 $http.get 方法中删除错误回调函数,那么它可以工作,即删除以下...

error(function (data) {
    $scope.message = 'There was an error with the data request.';
}

我不得不说,对于不是日常 JavaScript 程序员的人来说,Angular 确实是一条陡峭的学习曲线(尽管我似乎做得越来越多)。无论如何,感谢您的帮助 KatieK :-)

于 2012-12-04T11:11:03.703 回答