Ben*_*esh 13 dependency-injection facebook-javascript-sdk angularjs
我正在尝试为Angular创建一个facebook服务,这样我就可以更轻松地测试需要使用Facebook JS SDK和Graph API的代码.
这是我到目前为止所拥有的:
app.factory('facebook', function() {
return FB;
});
window.fbAsyncInit = function () {
FB.init({
appId: 'SOME_APP_ID_HERE', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true, // parse XFBML
oauth: true
});
};
// Load the SDK Asynchronously
(function (d) {
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) { return; }
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
})(document);
Run Code Online (Sandbox Code Playgroud)
现在,我知道实际的Facebook SDK部分正在运行......但在我的控制器中,引用始终为null.
在我的控制器中我只是有这样的东西:
function FooCtrl($scope, facebook) {
facebook.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
// do something
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}
Run Code Online (Sandbox Code Playgroud)
Angular然后抱怨它找不到facebookProvider.有关如何实现这一目标的任何想法?
用下面的数组括号括起您的工厂功能
app.factory('facebook', [function() {
return FB;
}]);
Run Code Online (Sandbox Code Playgroud)
API文档不够清晰.具有数组括号的点是您可以指定依赖项.它将在使用AUTO.$ inject创建服务时注入.但由于你没有依赖关系,它将跳过该任务:)
无论如何,如果你需要依赖关系,你可以像这样请求它们
app.factory('facebook', ["$log", function($someCrazyLoggerService){
$someCrazyLoggerService.log("I'm Auto Injected crazy Logger");
}]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6622 次 |
| 最近记录: |