document.ready使用angular.js在无限循环中调用

时间:2013-07-09 04:53:42

标签: javascript jquery node.js angularjs socket.io

我正在使用Angular.js,Node.js和Socket.io(以及其他东西)构建应用程序。我的问题是,当我尝试按照链接将我路由到登录页面时,我最终陷入无限循环。 jquery document.ready函数被反复调用,每次发生这种情况时,另一个套接字连接到用户。该页面甚至不会加载,因为这会不断被调用。我真的被困了,所以任何帮助都会非常感激。

以下是客户端路由的配置:

window.app = angular.module('MEAN', ['ngCookies', 'ngResource']);
window.app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/', { templateUrl: 'views/index.html' }).
    when('/signin', {
        templateUrl: '/partials/signin',
        controller: SigninCtrl
    }).
    when('/signup', {
        templateUrl: '/partials/signup',
        controller: SignupCtrl
    }).
    otherwise({redirectTo: '/'});
}]);

//Removing tomcat unspported headers
window.app.config(['$httpProvider', function($httpProvider, Configuration) {
    //delete $httpProvider.defaults.headers.common["X-Requested-With"];
}]);

//Setting HTML5 Location Mode
window.app.config(['$locationProvider', function($locationProvider) {
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix("!");
}]);

这是控制器:

function SigninCtrl($scope, $http, $location) {
$scope.form = {};
  $scope.title = "Sign In";
  $scope.SignIn = function() {
    $http.post('/users/session', $scope.form).
    success(function(data){
        console.log("Successful sign in", data);
        $location.path('/');
    })
    .error(function (data) {
        console.log("There was an error");
        $scope.errors = data.errors;
    });
  };
}

以下是我用于部分的玉模板:

extends ../layouts/default

block content
  .row
    .offset1.span5
      a(href="/auth/facebook")
        img(src="/img/icons/facebook.png")
      a(href="/auth/github")
        img(src="/img/icons/github.png")
      a(href="/auth/twitter")
        img(src="/img/icons/twitter.png")
      a(href="/auth/google")
        img(src="/img/icons/google.png")
    .span6
      if (typeof errors !== 'undefined')
        .fade.in.alert.alert-block.alert-error
          a.close(data-dismiss="alert", href="javascript:void(0)") x
          ul
            each error in errors
              li= error.type

      block auth
extends auth

block auth
  form.signin.form-horizontal(class="simple-form")
    .control-group
      label.control-label(for='email') Email
      .controls
        input#email(type='text', name="email", placeholder='Email')

    .control-group
      label.control-label(for='password') Password
      .controls
        input#password(type='password', name="password", placeholder='Password')

    .form-actions
      button(ng-click='SignIn()') Sign in
       
      | or 
      a.show-signup(href="/signup") Sign up

这是文档就绪功能:

window.bootstrap = function () {
    angular.bootstrap(document, ['MEAN']);
}
window.init = function () {
window.bootstrap();
}

window.connectSocket = function(){
  var socket = io.connect();
  socket.on('connect', function(message){
    console.log("socket connected");
  });
}

$(document).ready(function () {
    //Fixing facebook bug with redirect
  console.log("Document ready!");
    if (window.location.hash == "#_=_") window.location.hash = "";
    window.init();
    window.connectSocket();
});

1 个答案:

答案 0 :(得分:0)

我感到愚蠢,但我发现了这个问题。与此问题类似:What web server configuration is required to make AngularJS routing function correctly?

我实际上已经将路由从服务器移到了客户端,并且在部分模板中我已经包含了auth,而在auth文件中我有一个包含头文件模板的东西。最后,它试图在循环中包含相同的标题...希望这可能会帮助后来有同样问题的人。只要确保你没有多次包含标题......