2017-04-05 5 views
0

Une résolution est une propriété que vous pouvez associer à une route à la fois dans ngRoute et dans le routeur d'interface utilisateur plus robuste, mais la résolution ne peut pas fonctionner correctement. Impossible de charger les donnéesL'utilisation de Resolve dans les routes AngularJS ne fonctionne pas

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

Routes

.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

pouvez-vous me dire, pourquoi injectez-vous un message en tant que dépendance dans le contrôleur ?? – kaushlendras

+0

j'écris comme ça. app.controller ("homeController", la fonction ($ scope, Init, message) {$ scope.greeting = Init.getContacts; console.log ($ scope.greeting) –

Répondre

0

$cordovaContacts.find(options) est un appel et async déjà une promesse de retour. Ainsi, vous pouvez le retourner directement dans votre méthode - la méthode resolve peut gérer la promesse et renvoyer le résultat. Dans votre code, vous retournez contacts avant que l'appel soit terminé.

.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

pouvez-vous me partager un code –

+0

j'écris comme ceci app.controller ("homeController", fonction ($ scope, Init, message) { $ scope.greeting = Init.getContacts; console.log ($ scope.greeting); –