$ location.path()和$ routeProvider不工作

时间:2015-05-06 07:28:17

标签: javascript angularjs node.js sockets

我无法从log.html通过$ routeProvider和$ location.path()重定向到index.html并且添加了错误(log.html的控制器运行了一千次),有人可以让我知道什么是错的在这里:/

我的应用结构

node_modules
view
-log.html
-index.html
server.js
package.json

将log.html作为客户端

 <script type="application/javascript">
     socket = io.connect('http://localhost:1337/');
     angular.module('Log',['ngRoute'])

             .config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider){

                 $locationProvider.html5Mode(true).hashPrefix('!');
                 $routeProvider
                         .when('/main', {
                             templateUrl: '/view/index.html'
                         });
                 }])
             .controller('LogCtrl',['$scope','$location', function($scope,$location){

                         console.log('ok baby !!');

                         $scope.addUser = function(){
                             socket.emit('init',$scope.user);
                             $location.path("/main");
                         };

                     }]);


 </script>
<body  >
<div class=" main"  ng-controller = "LogCtrl" style = "width: 300px; ">
<fieldset>
     <form  ng-submit = "addUser()">
         <h2 >Hello!!</h2>
         <input ng-model = "user" required  style = "width: 200px; " placeholder = "Nhập tên của bạn" >
         <input type = "submit" value = "OK">
        <view></view>
    </form>
</fieldset>
</div>

和我的服务器端

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)



 , fs = require('fs');        

app.listen(1337);

function handler (req, res) {
  fs.readFile('./view/log.html',
  function (err, data) {
    if (err) {
      res.writeHead(500,{'Content-Type': 'text/html'});
      return res.end('Error loading log.html');
    }

res.writeHead(200);
res.end(data);
  });
}

1 个答案:

答案 0 :(得分:0)

我能做的是宣布主要状态

state('main', {
 url : '/',
 templateUrl : '/main.html'
}

你应该这样重定向

$location.path("/");