简体   繁体   English

立即在AngularJS上调用函数

[英]Calling function immedietaly at AngularJS

I am totally newbie to AngularJs. 我是AngularJs的新手。

I have pretty good example of ngClick which is calling a Facebook function. 我有一个很好的ngClick实例,它调用了Facebook函数。

I would like to change it and programmatically to call registerWithFacebook function after my controller will be ready. 我想更改它,并在控制器准备好后以编程方式调用registerWithFacebook函数。 What is the way for it? 这是怎么回事?

Here is the sample: http://jsfiddle.net/IgorMinar/Hxbqd/5/ 这是示例: http : //jsfiddle.net/IgorMinar/Hxbqd/5/

    angular.module('HomePageModule', []).service('facebookConnect', function($rootScope) {
        this.askFacebookForAuthentication = function(fail, success) {
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.api('/me', function() { $rootScope.$apply(success) });
                } else {
                   $rootScope.$apply(function() {
                     fail('User cancelled login or did not fully authorize.')
                   });
                }
            });
        }
});

function ConnectCtrl(facebookConnect, $scope, $resource) {

    $scope.user = {}
    $scope.error = null;

    $scope.registerWithFacebook = function() {
        facebookConnect.askFacebookForAuthentication(
        function(reason) { // fail
            $scope.error = reason;
        }, function(user) { // success
            $scope.user = user
        });
    }

    // Tried this but no success
    // $scope.registerWithFacebook();
}

ConnectCtrl.$inject = ['facebookConnect', '$scope', '$resource'];

Thanks 谢谢

If you want to load the FB library asynchronously, you need to let angular know after it initializes. 如果要异步加载FB库,则需要在初始化后让angular知道。 Here is one way to do it: 这是一种实现方法:

http://plnkr.co/edit/YO4duxL1Mh3dwYEEpprV?p=preview http://plnkr.co/edit/YO4duxL1Mh3dwYEEpprV?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM