在AngularJS路由中使用Resolve不起作用

时间:2017-04-05 15:32:02

标签: angularjs ionic-framework angular-routing

解析是您可以在ngRoute和更强大的UI路由器中附加到路由的属性。但是,解析无法正常工作。不加载数据

.factory("Init", function($cordovaContacts) {
    var contacts = []; //variable that holds contacts, returned from getContacts
    return {
        getContacts: function() {
            var options = {};
            options.filter = "";
            options.multiple = true;

            //get the phone contacts
            $cordovaContacts.find(options).then(function(result) {
                contacts = result;
            }, function(err) {});
            return contacts;
        }
    }
})

路线

 .state('app.home', {
        url: '/home',
        views: {
            'menuContent': {
                templateUrl: 'views/home/home.html',
                controller: 'homeController',
                resolve: {
                    getContacts: function(Init) {
                        return Init.getContacts();
                    }
                }
            }
        }
    })

Controller.js

app.controller("homeController", function (message) { $scope.greeting = getContacts; console.log($scope.greeting); });

2 个答案:

答案 0 :(得分:0)

$cordovaContacts.find(options)是异步调用,它已经返回了一个promise。因此,您可以直接在方法中返回它 - resolve方法可以处理promise并返回结果。在您的代码中,您将在通话结束前返回contacts

.factory("Init", function($cordovaContacts) {
    var contacts = []; //variable that holds contacts, returned from getContacts
    return {
        getContacts: function() {
            var options = {};
            options.filter = "";
            options.multiple = true;

            //get the phone contacts
            return $cordovaContacts.find(options);
        }
    }
})

答案 1 :(得分:0)

看看这个:https://github.com/angular-ui/ui-router/wiki。您需要将解析中定义的getContacts键注入控制器。