AngularJS加载服务,然后呼叫控制器和渲染 [英] AngularJS load service then call controller and render
本文介绍了AngularJS加载服务,然后呼叫控制器和渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是我需要的控制器被调用和模板得到呈现之前加载的服务。
http://jsfiddle.net/g75XQ/2/
My problem is that i need a service loaded before the controller get called and the template get rendered. http://jsfiddle.net/g75XQ/2/
HTML:
<div ng-app="app" ng-controller="root">
<h3>Do not render this before user has loaded</h3>
{{user}}
</div>
JavaScript的:
angular.module('app', []).
factory('user',function($timeout,$q){
var user = {};
$timeout(function(){//Simulate a request
user.name = "Jossi";
},1000);
return user;
}).
controller('root',function($scope,user){
alert("Do not alert before user has loaded");
$scope.user = user;
});
推荐答案
您可以使用手动初始化与 deffer角应用程序的初始化,而不是自动初始化NG-应用
属性
You can deffer init of angular app using manual initialization, instead of auto init with ng-app
attribute.
angular.element(document).ready(function() {
callService(function () {
angular.bootstrap(document);
});
});
其中, callService
是函数执行Ajax调用并接受成功回调,这将初始化角应用程序。
where callService
is your function performing ajax call and accepting success callback, which will init angular app.
这篇关于AngularJS加载服务,然后呼叫控制器和渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文