[AngularJS] Promise: promise chains, orginaze code
code angularjs Promise
2023-09-14 08:59:21 时间
angular.module( "FlightDemo", [ "FlightServices" ] ) .controller( "flightDashboard", FlighDashboard ); (function (angular) { "use strict"; angular.module("filghtServices", []) .service("user", function () { return{ email: "answer881215@gmail.comm", repository: "http://cs.uef.fi/paikka/zhentiw/personal" } }) .service("travelService", function (user, $q) { //Fight API (each returns a promise) return{ getDeparture: function (user) { var defer = $q.defer(); defer.resolve({ userID: user.email, flightID: "UA_21564", date: "01/14/2014 8: 00 AM" }); return defer.promise; }, getFlight: function(flightID){ return $q.resolve({ id: flightID, pilot: "Captain Morgan", plane: { make: "Boeing 717 RC", model: "TA-889" }, status: "onTime" }); } } }) .service("weatherService",function($q){ return{ getForecast: function(date){ return $q.resolve({ date: date, forecast: "rain" }) } } }); }(window.angular));
var FlighDashboard = function($scope, user, travelService, weatherService){ var loadDeparture = function(user){ return travelService .getDeparture(user.email) .then(function(){ $scope.departure = departure; return departure.flightID; }); //return an promise }, loadFlight = function(flightID){ return travelService .getFlight(flightID) .then(function(){ $scope.flight = flight; return flight; }); }, loadForecast = function(){ return weatherService .getForecast($scope.departure.date) .then(function(weather){ $scope.weather = weather; return weather; }); }; loadDeparture(user) .then(loadFlight) .then(loadForecast) .then("ALL finish"); $scope.user = user; $scope.departure = null; $scope.flight = null; $scope.weather = null; }
相关文章
- How to write perfect C code
- ubuntu 下出现E: Sub-process /usr/bin/dpkg returned an error code
- [AngularJS] Decorator pattern for code reuse
- [React] Asynchronously Load webpack Bundles through Code-splitting and React Suspense
- [Functional Programming Monad] Refactor Stateful Code To Use A State Monad
- python3:用pip安装第三方库时报错Command "python setup.py egg_info" failed with error code的解决
- Angularjs学习---angularjs环境搭建,ubuntu 12.04下安装nodejs、npm和karma
- [AngularJS] ocLazyLoad -- Lazy loaded module should contain all the dependencies code
- [HTTP] Understand 2xx HTTP Status Code Responses
- [D3] 13. Cleaner D3 code with selection.call()
- Not all code paths return a value
- 用ABAP 生成二维码 QR Code
- 如何让 Visual Studio Code 里显示 Cypress 的 intelligent code suggestion
- Atitit 发送post http 功能总结目录1. 常见三种模式 Form-data Form-urlencode Raw 12. Php code 12.1. urlencode
- Visual Studio Code 保存代码时报Applying code action Organize Imports
- Unable to install “Visual Studio Code”:snap “code” has “install-snap” change in progress
- 已解决ERROR:ssl_client_socket_impl.cc(992)] handshake failed; returned -1, SSL error code 1, net_error
- Bearpi开发板之Visual studio code终端编译
- VS code创建虚拟环境并安装Python包
- VS code代码函数体跳转快捷键
- entity framework code-frist config entity relationship