【问题标题】:ui router resolve chaining promisesui路由器解析链接承诺
【发布时间】:2014-09-16 08:17:45
【问题描述】:

使用 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();
              }
        }
})

它返回未定义。谁知道为什么?

【问题讨论】:

    标签: angularjs angular-ui-router resolve angular-promise


    【解决方案1】:

    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
    };
    

    【讨论】:

    • 不,它不起作用!我认为它会在第一次 defer.resolve 时返回它的工作原理
    • getDatas1 和 getDatas2 是否返回承诺? .. 如果是这样,那么 $q.all 也将返回一个承诺 .. 没有必要在 getData1AndData2 中使用 $q.defer()
    • 代码有点奇怪,因为你没有返回你得到的值。getData3Sources 不使用 getData1AndData2 'promise' 等中的'values'。
    • 是的,getDatas1 和 getDatas2 返回承诺。在 ui-router 解析中做出链接承诺的正确模式是什么?
    猜你喜欢
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多