角度配置块设置
我遇到了初始化Facebook和其他提供程序的这个问题,是否有可能以某种方式执行此代码而不是在应用程序启动时,但当用户进入我想要使用它的页面?我将从API调用中获取APP_ID,并且我不想在主页上加载页面x上所需的数据。角度配置块设置
.config(function(FacebookProvider) {
// Set your appId through the setAppId method or
// use the shortcut in the initialize method directly.
FacebookProvider.init('YOUR_APP_ID');
})
我认为要做到这一点的最好办法是使用服务,因为它是一个独立的,并不会被初始化多次。
Add a facebook service like this -
app.service('facebook', function(FacebookProvider) {
this.init: function(){
FacebookProvider.init('YOUR_APP_ID');
}
});
Then you can inject the facebook and call the init method like this -
app.controller('AuthCtrl', function($scope, fb) {
fb.init();
});
希望这有助于。
UPDATE
Apparently Providers can't be injected into services as they can only be accessed through the .config() block
解决方法
We can use the $injector object -
app.service('fb', function($injector) {
this.init: function(){
var FacebookProvider = $injector.get('FacebookProvider');
FacebookProvider.init('YOUR_APP_ID');
}
});
我无法注入FacebookProvider服务...未捕获的错误:[$ injector:unpr]未知的提供程序:FacebookProviderProvider Gatekeeper
请问您更新您的问题,你在用吗? – atefth
我创建的服务完全按照你的建议,但FacebookProvider只能用于配置我猜 - 看看这个https://github.com/Ciul/angular-facebook – Gatekeeper
@atefth选项是好的,但你也可以做它在路线的决心。
我觉得'.run()块'在特定的控制器将帮助你。 –
“但是当用户来到我想要使用它的页面时”=>把代码放在页面的控制器中......这是为了这个。 –
皮埃尔 - 问题是我不能在控制器中使用FacebookProvider – Gatekeeper