页面更改后角度控制器不起作用

时间:2014-01-28 09:04:14

标签: facebook angularjs

我有一个由自耕农生成的角度应用程序,它有两页。 我为这两个页面设置了一个routeProvider:

angular.module('fbPostApp', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ngRoute'
])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
    $routeProvider
      .when('/friend-filter', {
        templateUrl: 'views/friend-filter.html',
        controller: 'FriendFilterCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

MainCtrl:

function MainCtrl($scope,$http) {

        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'Myid',
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
            });
                $scope.login = function(){
                    FB.login(function(response) {
                        $scope.uid = response.authResponse.userID;
                        $scope.accessToken = response.authResponse.accessToken;
                        login_success($scope.uid,$scope.accessToken);
                    });
                }
        ................
            };
        };
    };

FriendFilterCtrl:

function FriendFilterCtrl($scope,$http) {

        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'Myid',
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
            });
                $scope.login = function(){
                    FB.login(function(response) {
                        $scope.uid = response.authResponse.userID;
                        $scope.accessToken = response.authResponse.accessToken;
                        login_success($scope.uid,$scope.accessToken);
                    });
                }
        ................
            };
        };
    }

我的链接:

<a href="#/main">Main</a>
<a href="#/friend-filter">Friend</a>

当我进入这两个页面时,他们的控制器工作得很好(使用你的Facebook登录)。但是,如果我通过链接“切换”到另一个页面,我可以获得页面的模板,但它不会做任何事情。 所以我想问一下,在这种情况下,我有什么错过或做错了吗?

1 个答案:

答案 0 :(得分:0)

将此代码放在任何角度控制器之外:

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'Myid',
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });
};

您的控制器现在应该如下所示:

angular.module('fbPostApp').controller('MainCtrl', ['$scope','$http', function ($scope,$http) {
    $scope.login = function(){
        FB.login(function(response) {
            $scope.uid = response.authResponse.userID;
            $scope.accessToken = response.authResponse.accessToken;
            login_success($scope.uid,$scope.accessToken);
        });
    }
    ................
}])

注意:如果您有全局Facebook登录按钮,则不应将$scope.login功能复制到应用中的每个控制器。了解angular directives

相关问题