我有类似的工厂与$ http调用里面如下:
appModule = angular.module('appModule', []);
appModule.factory('Search', function($rootScope, $http) {
var Search;
Search = {};
Search.types: ["bacon"];
Search.pastEvents = null;
$http.get('api/highlights').success(function(response) {
return Search.pastEvents = response.data;
});
return Search;
});
var notes_module = angular.module('notes', []);
notes_module.config(['$routeProvider', function ($routeProvider) {
var notes_promise = ['Notes', '$route', 'Search', function (Notes, $route, Search) {
//suspect that Search not having pastEvents ready in time of calling index method
//Notes resource
return Notes.index({subject_id: 1 }, Search);
}];
$routeProvider.when('/notes', {
templateUrl:'index.tpl.html',
controller:'NotesCtrl',
resolve:{
notes: notes_promise,
}
}); …Run Code Online (Sandbox Code Playgroud) 我想在我的AngularJS应用程序中实现promises,就像你在这个例子中看到的那样:https: //stackoverflow.com/a/12360882/371273
我的应用程序将采用多个数据阵列,在我的服务器上进行多次数据库命中,但我已将我的服务器配置为将所有数据作为一个响应返回,以最大限度地减少请求以保持我的应用程序尽可能高效(数据基本上是除非所有数据都到达客户端,否则无用).所以不要这样做......
MyCtrl.resolve = {
projects : function($q, $http) {
return $http.get('/api/projects'});
},
clients : function($q, $http) {
return $http.get('/api/clients'});
}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够向一个URL 发出一个单一的 $http GET请求/api/allData,然后projects将其设置为等于,并将对此allData.projects的承诺clients设置为allData.clients等等.(allData返回的数据在哪里)我的单一要求).我对承诺还很新,有人能给我一个例子,告诉我如何在里面设立这些承诺MyCtrl.resolve吗?
我开发了一个angularjs应用程序,我有一个REST API用于从我的角度应用程序中获取应用程序资源.当用户自己登录时,我将他的访问令牌保存在cookie中,如果他刷新页面我想从令牌中获取用户信息,如下所示:
mymodule.run(function ($rootScope, $cookies, AuthService, Restangular) {
if ($cookies.usertoken) {
// call GET api/account/
Restangular.one('account', '').get().then( function(user) {
AuthService.setCurrentUser(user);
});
}
});
Run Code Online (Sandbox Code Playgroud)
AuthService:
mymodule.factory('AuthService', function($http, $rootScope) {
var currentUser = null;
return {
setCurrentUser: function(user) {
currentUser = user;
},
getCurrentUser: function() {
return currentUser;
}
};
});
Run Code Online (Sandbox Code Playgroud)
但是如果用户访问需要用户变量的控制器:
mymodule.controller('DashboardCtrl', function (AuthService) {
var user = AuthService.getCurrentUser();
});
Run Code Online (Sandbox Code Playgroud)
控制器代码在api调用结束之前执行,所以我有一个null var.在启动控制器之前有没有一个好的解决方案等待用户数据加载?
我找到了这个链接,但我正在寻找一个更全局的方法来初始化应用程序上下文.
我无法弄清楚如何让routeProvider等到远程调用返回.我见过的最好的解决方案是这里的例子:延迟角度路线变化 .不幸的是,当我厌倦将该示例应用于我自己的代码时,绑定将在数据实际加载之前触发.有没有人知道另一个使用角度1.1.5的新资源语法的例子($ promise可以直接访问)?
这是我的代码的样子:
var productModule = angular.module('productModule', ['ngResource', 'ngLocale']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html',
controller: 'ResourceCtrl as appController' ,
resolve:
{
productData: function($resource)
{
console.log(["calling service"]);
return $resource(Services.ProductServices.PATH).query(null,
function(data){
console.log(["call succeeded"]);
},
function(data){
console.log(["calling failed"]);
}).$promise;
}
}
});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]) ;
productModule.controller('ResourceCtrl','$scope','productData',function($scope,productData) {
$scope.productData = productData;
console.log(["promise resolved"]);
}]);
Run Code Online (Sandbox Code Playgroud)
如果我运行该代码,控制台将显示:
我将使用$ http和JSON响应从服务器获取一些数据.$http.get()路线改变后调用.但是在下载数据之前会更改模板.我的目标是:
$scope.init()(via ng-init="init()"),这也初始化从服务器获取数据我怎样才能做到这一点?我的应用程序查找示例:
使用Javascript:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope, $http) {
$scope.init = function() {
$http({method: 'GET', url: 'http://ip.jsontest.com/'}).success(function(data) {
console.log(data);
$scope.ip = data.ip;
});
}
});
myApp.config(function($routeProvider) {
$routeProvider.when('/link', {
controller: 'MyCtrl',
templateUrl: 'embeded.tpl.html'
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<script type="text/ng-template" id="embeded.tpl.html">
<div ng-controller="MyCtrl" ng-init="init()">
Your IP Address is: {{ip}}
</div>
</script>
<div>
<ul>
<li><a href="#/link">change route</a></li>
</ul>
<div ng-view></div>
</div>
Run Code Online (Sandbox Code Playgroud) 在Angular中,$ routeProvider resolve属性允许延迟路由更改,直到加载数据.鉴于Ember中的路径模型钩子返回一个承诺,我想知道这些东西是如何在Ember中完成的
这就是我在角度延迟AngularJS路线变化中的意思,直到模型加载以防止闪烁
带有示例代码的链接会很棒
angularjs ×5
javascript ×3
api ×1
asynchronous ×1
callback ×1
ember-router ×1
ember.js ×1
html ×1
http ×1
promise ×1