我在弄清楚为什么我的承诺是不解析我希望它的方式有些问题。我使用的离子/ UI的路由。
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:
.state('tab.foo', {
url: '/foo',
views: {
'tab-foo': {
templateUrl: 'templates/tab-foo.html',
controller: 'fooCtrl as foo'
}
},
resolve: {
getData: function (Service) {
return Service.all();
}
}
})
在我的富控制器:
this.foo = getData;
当我这样做,富控制器是不确定的,任何想法,为什么?当loggig安慰我可以看到该服务由apps.js叫,我可以看到,因为我看到在控制台中的数据就得到解决。使用UI路由器我希望它不会加载该视图/控制器,直到一切都解决了。
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://odeto$c$c.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
有一个工作示例的。因为控制器将作为富,我们应该做的是这样的:
There is a working example. Because controller will be available as foo, we should do it like this:
控制器assignement到某些属性 myData的
The controller assignement into some property myData
.controller('fooCtrl', function($scope, getData) {
this.myData = getData;
})
模板调用将同时包含,控制器(富),并将其属性myData的
The template call would contain both, controller (foo) and its property myData
resolved stuff:
<pre>{{foo.myData | json}}</pre>
检查一下这里