简体   繁体   中英

$location.path() and $routeProvider not working

I can't redirect to index.html from log.html through $routeProvider and $location.path() and got errors from adding (controller of log.html run a thousand times), someone can let me know what wrong in here :/

My app structure

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

file log.html as client side

 <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>

and my server side

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);
  });
}

What I could do is declare a main state

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

And you should do the redirect this way

$location.path("/");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM