2017-04-05 70 views
0

下决心是可以附加在两个ngRoute路线和更强大的UI router.but决心不能运作良好的属性。无法加载数据使用解决AngularJS路线不工作

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

+0

你能告诉我,你为什么要注入的消息,在控制器的依赖? – kaushlendras

+0

我这样写。 app.controller( “HomeController的” 功能($范围,初始化,消息){$ scope.greeting = Init.getContacts;执行console.log($ scope.greeting) –

回答

0

$cordovaContacts.find(options)是一个异步调用,并已返回的承诺。所以,你可以在你的方法直接返回它 - resolve方法可以处理的承诺并返回结果。在你的代码正在返回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); 
     } 
    } 
}) 
+0

你可以分享我的一些代码 –

+0

我这样写 app.controller( “HomeController中”,功能($范围,INIT消息){$ = scope.greeting Init.getContacts; 的console.log($ scope.greeting); –