如何从另一个全局函数调用Angular $ rootScope方法?

时间:2017-11-26 09:02:23

标签: javascript angularjs facebook

基本上,我试图从Facebook Javascript SDK调用$ rootScope方法。 SDK代码直接放在我的HTML中,如下所示:

<script>
    window.fbAsyncInit = function() {
        // FB JavaScript SDK configuration and setup
        FB.init({
            appId: '<My App ID>', // FB App ID
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true, // parse social plugins on this page
            version: 'v2.8' // use graph api version 2.8
        });

        // Check whether the user already logged in
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                //display user data
                getFbUserData();
            }
        });
    };

    // Load the JavaScript SDK asynchronously
    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    // Facebook login with JavaScript SDK
    function fbLogin() {
        FB.login(function(response) {
            if (response.authResponse) {
                // Get and display the user profile data
                getFbUserData();
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        }, {
            scope: 'email'
        });
    }

    // Fetch the user profile data from facebook
    function getFbUserData() {
        FB.api('/me', {
                locale: 'en_US',
                fields: 'id,first_name,last_name,name,email,link,gender,locale,picture'
            },
            function(response) {
                **$rootScope.login(response);**
            });
    }

    // Logout from facebook
    function fbLogout() {
        FB.logout(function() {
            console.log('User has logged out.');
        });
    }
</script>

请注意,在我的getFbUserData方法中,我试图直接从这里调用名为login的rootScope方法。但是,这不起作用,因为它表示rootScope对象未定义或其他。这样做的正确方法是什么?我正在使用Angular1.5。

谢谢!

0 个答案:

没有答案