Ionic assigns the data returned asynchronously in the service to the controller's $scope

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.