Ron
Ron

Reputation: 103

Resolving promise with 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.

my service:

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

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

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

In my foo controller:

this.foo = getData;

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.

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

Upvotes: 3

Views: 5678

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123861

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

The controller assignement into some property myData

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

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

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

Check it here

Upvotes: 3

Related Questions