AngularJS: fabrik $ http service

Ich versuche zu verstehen, das Konzept der Fabrik und service in Winkel. Ich habe den folgenden code unter dem controller

init();

    function init(){
        $http.post('/services', { 
            type : 'getSource',
            ID    : 'TP001'
        }).
        success(function(data, status) {
            updateData(data);
        }).
        error(function(data, status) {

        });

        console.log(contentVariable);
    };
    function updateData(data){
        console.log(data);
    };

Dieser code funktioniert einwandfrei. Aber wenn ich mich bewege, $http-service in die Fabrik, ich bin nicht in der Lage, um die Daten zurück zu controller.

studentApp.factory('studentSessionFactory', function($http){
    var factory = {};
    factory.getSessions = function(){
        $http.post('/services', { 
            type : 'getSource',
            ID    : 'TP001'
        }).
        success(function(data, status) {
            return data;
        }).
        error(function(data, status) {

        });
    };
    return factory;
});

studentApp.controller('studentMenu',function($scope, studentSessionFactory){
    $scope.variableName = [];
    init();
    function init(){
        $scope.variableName = studentSessionFactory.getSessions();
        console.log($scope.variableName);
    };
});

Gibt es einen Vorteil, mit der Fabrik, da $http funktioniert auch unter controller

InformationsquelleAutor der Frage de-bugged | 2013-04-26

Schreibe einen Kommentar