2015-05-05 91 views
1

我是AngularJs和nodejs的新手,我有一个简单的聊天应用程序,角色作为客户端和socketio作为服务器端。我得到了加载角页面时出现错误

Error: [$injector:unpr] Unknown provider: socketProvider <- socket <- LogCtrl 

我的客户端

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.min.js"></script> 
<script src="http://localhost:1337/socket.io/socket.io.js"></script> 

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

      .controller('LogCtrl',['$scope','socket','$location', function($scope,socket,$location){ 

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

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

      }]); 
</script> 

<body ng-app = "Log" > 
<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"> 
    </form> 
</fieldset> 
</div> 

请帮我


我尝试重定向到指数有什么错我的客户.html加入一些代码

.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider){ 
       $routeProvider. 
          when('/main', { 
           templateUrl: 'view/index.html', 
           controller: 'ChatCtrl' 
          }) 
       $locationProvider.html5Mode({ 
        enabled: true, 
        requireBase: false 
       }); 
       }]) 

      .controller('LogCtrl',['$scope','$location', function($scope,$location){ 

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

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

,但它不能正常工作,送花儿给人重新加载侧(log.html)

回答

1

插座不应该在控制器的依赖性在那里,因为它是你已经在脚本之外声明的全局变量。服务,工厂,提供,常量,值等只能用于注入依赖。

代码

.controller('LogCtrl', ['$scope', '$location', function($scope, $location) { 
    console.log('ok baby !!'); 

    $scope.addUser = function() { 
     socket.emit('init', $scope.user); 
     $location.path("/view/index.html"); 
    }; 
}]); 
+0

韩国社交协会很多,你救我一命:v –

+1

正是我需要它来修复它 – Xvegas

相关问题