0

我正在使用 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();
});
4

1 回答 1

0

我觉得很愚蠢,但我发现了问题所在。与此问题类似:使 AngularJS 路由正常运行需要哪些 Web 服务器配置?

实际上,我之前将路由从服务器移动到客户端,在部分模板中我包含了身份验证,在身份验证文件中我有一个包含标题模板的内容,Angular 已经做了。最后它试图在一个循环中包含相同的标题......希望这可能会帮助以后遇到同样问题的人。只要确保您没有多次包含标题...

于 2013-07-13T15:19:40.837 回答