$stateProvider.state('myState', { resolve:{ // Example using function with simple return value. // Since it's not a promise, it resolves immediately. simpleObj: function(){ return {value: 'simple!'}; }, // Example using function with returned promise. // 这是一种典型使用方式 // 请给函数注入任何想要的服务依赖,例如 $http promiseObj: function($http){ // $http returns a promise for the url data return $http({method: 'GET', url: '/someUrl'}); }, // Another promise example. // 如果想对返回结果进行处理, 可以使用 .then 方法 // 这是另一种典型使用方式 promiseObj2: function($http){ return $http({method: 'GET', url: '/someUrl'}) .then (function (data) { return doSomeStuffFirst(data); }); }, // 使用服务名的例子,这将在模块中查找名称为 'translations' 的服务,并返回该服务 // Note: The service could return a promise and // it would work just like the example above translations: "translations", // 将服务模块作为解决函数的依赖项,通过参数传入 // 提示:依赖项 $stateParams 代表 url 中的参数 translations2: function(translations, $stateParams){ // Assume that getLang is a service method // that uses $http to fetch some translations. // Also assume our url was "/:lang/home". translations.getLang($stateParams.lang); }, // Example showing returning of custom made promise greeting: function($q, $timeout){ var deferred = $q.defer(); $timeout(function() { deferred.resolve('Hello!'); }, 1000); return deferred.promise; } }, // 控制器将等待上面的解决项都被解决后才被实例化 controller: function($scope, simpleObj, promiseObj, promiseObj2, translations, translations2, greeting){ $scope.simple = simpleObj.value; // 这里可以放心使用 promiseObj 中的对象 $scope.items = promiseObj.items; $scope.items = promiseObj2.items; $scope.title = translations.getLang("english").title; $scope.title = translations2.title; $scope.greeting = greeting; } })