1. Get data instance asynchronously in service
Angular.module (' starter.services ', []). Factory (' Chats ',function($http, $q) {//define service for chats return{all:function() {//The all method gets the data asynchronously varDeferred= $q. Defer ();//Define deferred varPromise=deferred.promise;//Define Promise$http. Get (' http://localhost:3000/lists '). Success (function(data,status,headers,config) {//executes the Deferred.resolve method, passing in the returned data as a parameterdeferred.resolve (data); }) //Back to Promise returnpromise; } };});
2, the controller executes chats's All method, and assigns the returned data to the $scope's items, where result is the data returned asynchronously in the service's All method
Angular.module (' starter.controllers ', []). Controller (function($scope, Chats) { Chats.all (). Then (function(result) { = result; },function() { Console.log (' goodsservice get error '); } )
Ionic assigns the data returned asynchronously in the service to the controller's $scope