1

データが相互に依存している ui-router を使用して状態を変更するときにデータが必要になるため、連鎖プロミスを使用する必要があります

svc.getData1AndData2 = function(){
        var defer = $q.defer();
        $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) {
            $rootScope.datas2 = values[1];
            $rootScope.datas1 = values[0];
            defer.resolve(values);
        }, function(error) {
            console.log(error);
            defer.reject(error);
        });
    return defer.promise;

svc.getData3Sources = function(){
        svc.getData1AndData2().then(function(value){
            return svc.getSources(24);
        })
    };

svc.getSources=function(id){
        var defer = $q.defer();
        Services.query({Id:id}, function(data){
                defer.resolve(data);
            };
    };

そして私の状態は

.state('secure.list', {
       url: "/list",
       templateUrl: "views/list.html",
       controller: 'ListCtrl',
       resolve: {
              InitData: function (InitFactory) {
              return InitFactory.getData3Sources();
              }
        }
})

undefined を返します。誰が理由を知っていますか?

4

1 に答える 1

0

svc.getData3Sources は何も返さない..

試す ...

svc.getData1AndData2 = function(){
    // no need to use $defer here

    var promise = $q.all([svc.getDatas1(),svc.getDatas2()]).then(function(values) {
        $rootScope.datas2 = values[1];
        $rootScope.datas1 = values[0];

        return values;
    }, function(error) {
        console.log(error);
        defer.reject(error);
    });

    return promise;
}; // this line was missing

svc.getData3Sources = function(){
    var promise = svc.getData1AndData2().then(function(values){ 
        // changed value to values in the line above 
        // as the promise in getData1AndData2 resolves  ... valueS

        // why don't you use the values from getData1AndData2 ?? 
        return svc.getSources(24);
    });

    return promise;
};

svc.getSources=function(id){
    var defer = $q.defer();

    // it looks like you're having to use $q.defer 
    // as your Service.Query takes a callback rather than returning a promise
    // if you control this then a promise would be much better
    Services.query({Id:id}, function(data){
            defer.resolve(data);
        }); // the bracket ')' here was missing

    return defer.promise; // this line was missing
};
于 2014-07-24T17:15:41.680 に答える