angularjs:从服务返回json数据到 Controller

标签 angularjs xmlhttprequest

我正在使用 Angular JS 和 Spring MVC 来开发 Web 应用程序。

它正在访问 Spring MVC Rest 服务并将数据引入 Angular JS 服务,但我无法在服务中看到相同的数据。

Controller .js

app.controller('CustomersController', function($scope, customersService){

init();
function init(){
    var data =customersService.getCustomers();
    $scope.customers = data;
}

$scope.insertCustomer = function(){
    var firstName = $scope.newCustomer.firstName;
    var lastName = $scope.newCustomer.lastName;
    var city = $scope.newCustomer.city;
    customersService.insertCustomer(firstName, lastName, city);
    $scope.newCustomer.firstName = '';
    $scope.newCustomer.lastName = '';
    $scope.newCustomer.city = '';
};

$scope.deleteCustomer = function(id){
    customersService.deleteCustomer(id);
};
});

app.controller('NavbarController', function ($scope, $location) {
    $scope.getClass = function (path) {
        if ($location.path().substr(0, path.length) == path) {
            return true;
        } else {
            return false;
        }
    };
});

customersService.js

app.service('customersService', function($http, $log) {

this.getCustomers = function() {
    $http({
        method : 'POST',
        url : '/CustomerManagementApp/customers/all'
    }).success(function(data, status, headers, config) {
        $log.log('Done');
        angular.forEach(data, function(c) {
            $log.log(c.firstName);
        });
        customers = data;
        return customers;
    });     
};

this.insertCustomer = function(firstName, lastName, city) {
    var topID = customers.length + 1;
    customers.push({
        id : topID,
        firstName : firstName,
        lastName : lastName,
        city : city
    });
};

this.getCustomer = function(id) {

};

this.deleteCustomer = function(id) {

};

var customers = {};
});

我可以在服务中使用 forEach 循环打印所有数据。但在 Controller 中它显示空对象数组 Object { }

Firebug 控制台没有错误。我正在使用 POST 方法。如果需要任何其他代码,请告诉我。

最佳答案

当然。试试这个代码

function init(){
    customersService.getCustomers().then(function(response){
        $scope.customers = response.data;
    });
}

this.getCustomers = function() {
    var promise = $http({
        method : 'POST',
        url : '/CustomerManagementApp/customers/all'
    }).success(function(data, status, headers, config) {
        $log.log('Done');
        angular.forEach(data, function(c) {
            $log.log(c.firstName);
        });
        customers = data;
        return customers;
    });    

    return promise; 
};

promise - as an interface for interacting with an object that represents the result of an action that is performed asynchronously, and may or may not be finished at any given point in time.

关于angularjs:从服务返回json数据到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18704638/

相关文章:

angularjs - 人行横道的替代方案

php - 当 codeigniter/tank auth session 超时时处理 ajax 调用中的 php 重定向

javascript - 为什么异步 XMLHttpRequests 优于同步请求?

javascript - 如何制作完整的 POST XMLHttpRequest?

javascript - 通过文本搜索和下拉列表进行 Angular 过滤

javascript - 相对于 .js 文件的 Angular 指令 templateUrl

javascript - XMLHttpRequest (Ajax) 错误

javascript - 检测 Ajax 请求是否在 JavaScript 中失败

javascript - 如何使用 AngularJS 的 $filter 按年和月对数据进行分组

javascript - 从嵌套指令调用 Controller $scope 方法