angularjs中的$ http.get显示" TypeError:boolean不是函数"

时间:2014-03-25 15:17:14

标签: angularjs

我在工厂中使用angularjs $http.get来执行API调用。当我运行angularjs应用程序时,它返回数据。但是,当我使用F12查看控制台选项卡时,它有:

"TypeError: boolean is not a function"

此错误由angular.js文件生成。请参阅下面的工厂代码和listController代码。

有没有人知道为什么以下代码会导致angular.js生成:

"TypeError: boolean is not a function" and how to fix the following code to resolve this error?

以下是工厂中的代码:

app.factory('MyFactory', function ($http) {
    var dataFactory = {};
    var urlBase = 'api/suppliers/search?&q=';
    dataFactory.search = function (searchString) {
        var urlString = urlBase + searchString;
        return $http.get(urlString)
    }
    return dataFactory;
});

以下是调用工厂的代码:

var ListController = function ($scope, $location, MyFactory) {

    $scope.message = "List.html page";

    var searchstring = 'de';

    getSuppliers();

    function getSuppliers() {
        MyFactory.search(searchstring)
            .success(function (data) {
                $scope.suppliers = [];
                $scope.suppliers = data.items;
            })
            .error(function (error) {
                $scope.status = "Unable to load customer data: " + error.message;
            });
    }

};

1 个答案:

答案 0 :(得分:1)

$http返回一个承诺。您需要致电then()并将回调传递给它

function getSuppliers() {
    MyFactory.search(searchstring).then(function (data) {
       $scope.suppliers = [];
       $scope.suppliers = data.items;
    },function (error) {
       $scope.status = "Unable to load customer data: " + error.message;
    });
}