使用 ionic/ui-routing 解决 promise [英] Resolving promise with ionic/ui-routing

查看:17
本文介绍了使用 ionic/ui-routing 解决 promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚为什么我的承诺没有按照我期望的方式解决时遇到了一些问题.我正在使用 Ionic/ui-routing.

I'm having some problems figuring out why my promise isn't resolving the way I would expect it to. I'm using Ionic/ui-routing.

我的服务:

return {
    all: function () {
        $localForage.getItem('foo').then(function (bar) {
            return bar;
        });
    }
};

当我将数据记录到控制台时,我看到 localForage 从 indexeddb 返回的对象.

When I log data to console, I see the objects that localForage returns from indexeddb.

我的 apps.js:

my apps.js:

.state('tab.foo', {
    url: '/foo',
    views: {
        'tab-foo': {
            templateUrl: 'templates/tab-foo.html',
            controller: 'fooCtrl as foo'
        }
    },
    resolve: {
        getData: function (Service) {
            return Service.all();
        }
    }
})

在我的 foo 控制器中:

In my foo controller:

this.foo = getData;

当我这样做时,控制器中的 foo 是未定义的,有什么想法吗?当loggig到控制台时,我可以看到该服务被apps.js调用,我可以看到它已解决,因为我在控制台中看到了数据.使用 ui-router 我希望它在一切都解决之前不会加载视图/控制器.

When I do this, foo in the controller is undefined, any ideas why? When loggig to console I can see that the service is called by apps.js, I can see that it is resolved because I see the data in the console. Using ui-router I would expect it wouldn't load the view/controller until everything is resolved.

我使用以下作为灵感,只是没有 $q 服务.http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

I used the following as inspiration, only without the $q service. http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

推荐答案

有一个工作示例.因为控制器将作为 foo 可用,我们应该这样做:

There is a working example. Because controller will be available as foo, we should do it like this:

控制器赋值给一些属性myData

.controller('fooCtrl', function($scope, getData) {
  this.myData = getData;
})

模板调用将包含控制器 (foo) 及其属性 myData

The template call would contain both, controller (foo) and its property myData

resolved stuff:
<pre>{{foo.myData | json}}</pre>

此处查看

这篇关于使用 ionic/ui-routing 解决 promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆