2017-04-05 73 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); –