我认为要做到这一点的最好办法是使用服务,因为它是一个独立的,并不会被初始化多次。
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');
}
});
我觉得'.run()块'在特定的控制器将帮助你。 –
“但是当用户来到我想要使用它的页面时”=>把代码放在页面的控制器中......这是为了这个。 –
皮埃尔 - 问题是我不能在控制器中使用FacebookProvider – Gatekeeper