2013-04-01 19 views
0

我完全是AngularJs的新手。在AngularJS上調用函數immedietaly

我有一個非常好的ngClick調用Facebook函數的例子。

我想改變它並以編程方式在我的控制器準備就緒後調用registerWithFacebook函數。它的方式是什麼?

下面是示例: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']; 

感謝

+0

你說的「頁面加載」的意思具體是什麼?爲什麼在這一點上需要運行? –

+0

我只是在練習,並且想手動撥打registerWithFacebook。通過「頁面加載」,我的意思就是像這樣的$ scope。$ on('$ viewContentLoaded',registerWithFacebook);'但它不能工作 – Cemo

+0

@JoshDavidMiller我也編輯了我的問題,說明我想要更好。謝謝。 – Cemo

回答