[AngularJS] Promises in config. 2. config().when()
in angularjs config when promises
2023-09-14 08:59:21 时间
Before we see how promise works in contrller. But in contrller it is not a good way to put promises.
When you route the page, before the controller get excuted and templated loaded, you can use 'resolve' keyword in config or service or factory.
Here we show how to use promise in config.
'resolve' get loaded before controller and template, good to load $http stuff.
resolve: {
//appXXX is the ng-app appXXX: function($q, $timeout){ //inject two services, function can inject services!!! var defer = $q.defer(); $timeout(function(){defer.resolve()}, 2000); return defer.promise // it is important to return the defer.promises .then(console.log("logout 1")) .then(console.log("logout 2")); } },
var app = angular.module("app", ["ngRoute"]); app.config(function($routeProvider) { $routeProvider .when('/', { resolve: { doFirst: function($q, $timeout){ var defer = $q.defer(); $timeout(function(){defer.resolve()}, 2000); return defer.promise .then(console.log("logout 1")) .then(console.log("logout 2")); } }, templateUrl: "app.html", controller: "AppCtrl" }) }); app.controller('AppCtrl',function($scope){ $scope.model = {message: "It is great!"} });
相关文章
- Angularjs学习---angularjs环境搭建,ubuntu 12.04下安装nodejs、npm和karma
- [Angular] Use Angular components in AngularJS applications with Angular Elements
- [AngularJS] Interpolation fail in IE 11
- [Preact] Use State and Props in the Component Render Function
- [AngularJS] New in Angular 1.5 ng-animate-swap
- [AngularJS] New in Angular 1.3 - bindToController
- [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled
- [AngularJS] Accessing Data in HTML -- controllerAs, using promises
- Python version 2.7 required, which was not found in the registry解决方法
- [Angular] Use Angular components in AngularJS applications with Angular Elements
- [AngularJS] Interpolation fail in IE 11
- [AngularJS] Write a simple Redux store in AngularJS app
- [TypeScript] Using Interfaces to Describe Types in TypeScript
- [AngularJS] Transforming raw JSON data to meaningful output in AngularJS
- [AngularJS] New bindToController in AngularJS 1.4
- [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled
- [AngularJS] Promises in controller. 1. $q.defer(), defer.resolve(), defer.reject(), promise.then().then()
- [AngularJS] Transclude -- using what existing in DOM to replace the template elements in directive
- 宝塔部署项目报Warning: require(): open_basedir restriction in effect的解决方案
- Document Builder: directly change element in Webdynpro ui
- Cannot start debugging initial download in ERP
- 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法
- Spark SQL 源代码分析之 In-Memory Columnar Storage 之 in-memory query