2017-03-20 2 views
0

저는 angularJS를 처음 사용하는데이 오류가 왜 발생하는지 이해할 수 없었습니다. 내가 실수 한 부분을 찾도록 도와주세요.오류 : ng : areq 잘못된 인수 인수 'msgController'는 정의되지 않은 함수입니다.

index.html을

<!DOCTYPE html> 
<html ng-app="MyApp"> 
<head> 
    <title>Angular JS Web-Socket</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
</head> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script> 
<script src="js/chat.js"></script> 
<script src="lib/ngDialog.js"></script> 

<link rel="stylesheet" type="text/css" href="css/ngDialog-theme-default.css"> 
<link rel="stylesheet" type="text/css" href="css/ngDialog.css"> 

<body> 
    <button ng-controller="MainCtrl" ng-click="openChatBox()">Open</button> 
</body> 
</html> 

chatBox.html

<script src="lib/ngDialog.js"></script> 
<script src="lib/angular-websocket.js"></script> 
<script src="js/socket.js"></script> 
<link rel="stylesheet" type="text/css" href="css/ngDialog.css"> 
<link rel="stylesheet" type="text/css" href="css/chat.css"> 
<!-- The Modal --> 

<div id="myModal" class="modal" ng-app="chatSocket"> 

    <!-- Modal content --> 
<div class="modal-content"> 
    <div class="modal-header"> 
    <div class="menu"> 
     <div class="name">Bot</div> 
     <div class="close">Close</div> 
    </div> 
</div> 
<div class="modal-body" ng-controller="msgController"> 
<h2>Modal body</h2> 
    <label ng-repeat="item in socket.msg"> 
     Name : {{item.name}} <br> 
     Msg : {{item.msg}} 
    </label> 
</div> 
<div class="modal-footer"> 
    <h3>Modal Footer</h3> 
    <form ng-submit="submit()"> 
    <input type="text" ng-model="msgbox">    
    <button id="sendMsg" type="submit" >Send Message</button> 
    </form> 
</div> 

chat.js

var app = angular.module('MyApp', ['ngDialog']); 
app.controller('MainCtrl', function ($scope, ngDialog) { 
$scope.openChatBox = function() { 
    ngDialog.openConfirm({template: 'chatBox.html', 
     scope: $scope //Pass the scope object if you need to access in the template 
    }).then(
     function(value) { 
      //You need to implement the saveForm() method which should return a promise object 
      $scope.closeChat().then(

      ); 
     }, 
     function(value) { 
      //Cancel or do nothing 
     } 
    ); 
}; 
}); 

socket.js 내가 msgController을 사용하지 않은 경우, 대화 상자가 개방되어

angular.module('chatSocket', ['ngWebSocket']) 
.factory('socket', function ($websocket) { 

    // Open a WebSocket connection 
    var ws = $websocket("ws://" + document.location.host + document.location.pathname); 
    var msg = []; 

    ws.onMessage(function (event) { 
     console.log('message: ', event.data); 
     var response; 

     try { 
      response = event.data; 
     } catch (e) { 
      console.log('error: ', e); 
     } 

     msg.push({ 
      name: "Bot", 
      msg: response, 
     }); 
    }); 

    ws.onError(function (event) { 
     console.log('connection Error', event); 
    }); 

    ws.onClose(function (event) { 
     console.log('connection closed', event); 
    }); 

    ws.onOpen(function() { 
     console.log('connection open'); 
     ws.send('HELLO SERVER'); 
    }); 

    return { 
     msg: msg, 
     status: function() { 
      return ws.readyState; 
     }, 

     send: function (message) { 
      console.log(message); 
      msg.push({ 
       name: "User", 
       msg: message, 
      }); 
      ws.send(message); 
     } 
    }; 
}) 

.controller('msgController', function ($scope, socket) { 
$scope.socket = socket; 
$scope.submit = function() { 
    socket.send($scope.msgbox); 
}; 
}); 

. 이것을 포함하면 오류가 표시되고 대화 상자를 열 수 없습니다.

+0

당신은 첫 번째 모듈'VAR에 대한 종속성으로 두 번째 모듈'chatSocket'을 참조하지 않았다

당신은 MyApp 모듈에 의존성과 chatSocket을 지정하고 타격과 같은 msgControllerMyApp에 모듈을 정의해야합니다 app = angular.module ('MyApp', [ 'ngDialog', 'chatSocket']); ' –

답변

1

openConfirm 방법을 ngDialog으로 참조 했으므로 사용자가 정의하지 않았습니다.

ngDialog.openConfirm({ 
    template: 'chatBox.html', 
    controller: 'msgController', 
    scope: $scope //Pass the scope object if you need to access in the template 
}) 

그 외에도 당신은 chatSocket 모듈에 msgController을 지정하고 당신은 당신이 오류가 발생하는 이유입니다 MyApp 모듈을 사용하고 있습니다.

var app = angular.module('MyApp', ['ngDialog','chatSocket']); 

app.controller('msgController', function ($scope, socket) { 
$scope.socket = socket; 
$scope.submit = function() { 
    socket.send($scope.msgbox); 
}; 
}); 
+0

MyApp 모듈에서 msgController를 정의하면 다음 오류가 발생합니다. "오류 : $ injector : unpr 알 수없는 공급자"https://docs.angularjs.org/error/$injector/unpr?p0=MainCtrlProvider%20%3C-%20MainCtrl%20%3C-%20msgController – Kavipriya

+0

@Kavipriya is that 왜냐하면'msgController'에서 chatSocket' 모듈에 정의 된'socket' 팩토리를 참조하기 때문입니다. 따라서이 문제를 해결하기 위해'MyApp' 모듈에 대한 의존성을 지정할 수 있습니다. 업데이트 된 답변보기 –

+0

그래도 그 일을 해 봤지만 도움이되었습니다. 이 오류가 나타납니다. "오류 : $ injector : modulerr 모듈 오류" – Kavipriya

관련 문제