为什么我的角度js代码不起作用

时间:2017-03-03 16:12:57

标签: angularjs debugging

此代码用于从我的web api获取数据,但它不起作用。我一次又一次地检查,但注意到了。请帮帮我,我是否提错了代码?

var MyApp = angular.module("MyApp", []);
    MyApp.factory('Service', ['$http', function ($http) {
        var urlBase = 'http://localhost:1883/api';
        var datafactory = {};
        Service.getCustomers = function () {
            return $http.get(urlBase + '/Customers1');
        };
        return datafactory;
    }]);

    MyApp.Controller('UpdateController', ['$scope', 'datafactory', function ($scope, datafactory) {
        getCustomers();

        function getCustomers() {
            datafactory.getCustomers()
            .then(function (data) {
                $scope.customers = data;
            }, function (error) {
                $scope.status = 'Unable to load customer data: ' + error.message;
            });
        }
    }]);

这是浏览器的错误: enter image description here

1 个答案:

答案 0 :(得分:0)

有一个拼写错误,你返回一个空对象。将Service.getCustomers更改为datafactory.getCustomers