Frage

Ich habe diesen Code:

PostApp.factory('loadPage', function ($http) {
return {
    loadOtherPage: function (page, status, permition, order, cultureId) {
        $http({
            url: '/Administrator/Post/PagedIndex',
            method: "POST",
            data: { page: page, status: status, permition: permition, order: order, cultureId: cultureId }
        }).success(function(data) {
            return  data;
        });

    }
};

});

PostApp.controller('PostController',
function ($scope, $http, loadPage) {
    $scope.status = 'Published';
    $scope.permition = 'Global';
    $scope.order = 'Asscending';
    $scope.cultureId = 1;
    $scope.ListOfItems = [];
    $scope.start = 2;
    $scope.skip = 10;

    $scope.loaddata = function () {
        $scope.ListOfItems = loadPage.loadOtherPage($scope.start, $scope.status, $scope.permition, $scope.order, $scope.cultureId);
    };
}

);

Aber legen Sie die Antwort der Dienste „loadPage.loadOtherPage“ nicht auf die Variable „$scope.ListOfItems“ fest.Die Antwort ist JSON in der Konsole des Browsers:

[{"PId":15,"Id":15,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/19/2013","Title":"xxxxxxxxxxxx","CultureId":1,"Username":"naser","CommentCount":0},{"PId":16,"Id":16,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/19/2013","Title":"yyyyyyyyyyyyyyyyyy","CultureId":1,"Username":"naser","CommentCount":0},{"PId":17,"Id":17,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/21/2013","Title":"zzzzzzzzzzzzzzzz","CultureId":1,"Username":"naser","CommentCount":0}]

final $scope.ListOfItems ist leer?

War es hilfreich?

Lösung

(BEARBEITET:Hinzufügen von Variablen für zusätzliche Klarheit)

Der loadOtherPage Funktion gibt nichts zurück, deshalb $scope.ListOfItems ist leer.Der richtige Weg, dies zu tun, ist etwa:

loadOtherPage: function (page, status, permition, order, cultureId) {
    var httpPromise =  $http({
        url: '/Administrator/Post/PagedIndex',
        method: "POST",
        data: { ... }
    });
    return httpPromise;
}

Sie geben im Wesentlichen Ihr Versprechen zurück $http kehrt zurück zum Anrufer.Ihr Controller sollte sein:

$scope.loaddata = function () {
    var loadPagePromise = loadPage.loadOtherPage( ... );
    loadPagePromise.success(function(data) {
        $scope.ListOfItems = data;
    });
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top