/api/

app.js
'use strict';
/* App Module */
var springChat = angular.module('springChat', ['springChat.controllers',
'springChat.services',
'springChat.directives']);
contrillers.js
'use strict';
/* Controllers */
angular.module('springChat.controllers', ['toaster'])
.controller('ChatController', ['$scope', '$location', '$interval', 'toaster', 'ChatSocket', function($scope, $location, $interval, toaster, chatSocket) {
var typing = undefined;
$scope.from = '';
$scope.sendTo = 'everyone';
$scope.participants = [];
$scope.messages = [];
$scope.newMessage = '';
$scope.sendMessage = function() {
var destination = "/app/chat";
if($scope.sendTo != "everyone") {
destination = "/app/private." + $scope.sendTo;
$scope.messages.unshift({message: $scope.newMessage, from: 'you', priv: true, to: $scope.sendTo});
}
chatSocket.send(destination, {}, JSON.stringify({message: $scope.newMessage}));
$scope.newMessage = '';
};
$scope.startTyping = function() {
// Don't send notification if we are still typing or we are typing a private message
if (angular.isDefined(typing) || $scope.sendTo != "everyone") return;
typing = $interval(function() {
$scope.stopTyping();
}, 500);
chatSocket.send("/topic/chat.typing", {}, JSON.stringify({from: $scope.from, typing: true}));
};
$scope.stopTyping = function() {
if (angular.isDefined(typing)) {
$interval.cancel(typing);
typing = undefined;
chatSocket.send("/topic/chat.typing", {}, JSON.stringify({from: $scope.from, typing: false}));
}
};
$scope.privateSending = function(from) {
$scope.sendTo = (from != $scope.sendTo) ? from : 'everyone';
};
var initStompClient = function() {
chatSocket.init('/ws');
chatSocket.connect(function(frame) {
$scope.from = frame.headers['user-name'];
chatSocket.subscribe("/app/participants", function(message) {
$scope.participants = JSON.parse(message.body);
});
chatSocket.subscribe("/topic/chat.login", function(message) {
$scope.participants.unshift({from: JSON.parse(message.body).from, typing : false});
});
chatSocket.subscribe("/topic/chat.logout", function(message) {
var from = JSON.parse(message.body).from;
for(var index in $scope.participants) {
if($scope.participants[index].from == from) {
$scope.participants.splice(index, 1);
}
}
});
chatSocket.subscribe("/topic/chat.typing", function(message) {
var parsed = JSON.parse(message.body);
if(parsed.from == $scope.from) return;
for(var index in $scope.participants) {
var participant = $scope.participants[index];
if(participant.from == parsed.from) {
$scope.participants[index].typing = parsed.typing;
}
}
});
chatSocket.subscribe("/topic/message", function(message) {
$scope.messages.unshift(JSON.parse(message.body));
});
chatSocket.subscribe("/user/exchange/amq.direct/message", function(message) {
var parsed = JSON.parse(message.body);
parsed.priv = true;
$scope.messages.unshift(parsed);
});
chatSocket.subscribe("/user/exchange/amq.direct/errors", function(message) {
toaster.pop('error', "Error", message.body);
});
}, function(error) {
toaster.pop('error', 'Error', 'Connection error ' + error);
});
};
initStompClient();
}]);
services.js
'use strict';
/* Services */
angular.module('springChat.services', [])
.factory('ChatSocket', ['$rootScope', function($rootScope) {
var stompClient;
var wrappedSocket = {
init: function(url) {
stompClient = Stomp.over(new SockJS(url));
},
connect: function(successCallback, errorCallback) {
stompClient.connect({}, function(frame) {
$rootScope.$apply(function() {
successCallback(frame);
});
}, function(error) {
$rootScope.$apply(function(){
errorCallback(error);
});
});
},
subscribe : function(destination, callback) {
stompClient.subscribe(destination, function(message) {
$rootScope.$apply(function(){
callback(message);
});
});
},
send: function(destination, headers, object) {
stompClient.send(destination, headers, object);
}
}
return wrappedSocket;
}]);
directives.js
/* Directives */
angular.module('springChat.directives', [])
.directive('printMessage', function () {
return {
restrict: 'A',
template: '<span ng-show="message.priv">[private] </span><strong>{{message.from}}<span ng-show="message.to"> -> {{message.to}}</span>:</strong> {{message.message}}<br/>'
};
});
&#13;
当我尝试运行我显示的聊天应用程序时,
angular.js:38未捕获错误:[$ injector:modulerr] http://errors.angularjs.org/1.3.15/ $ injector / modulerr?p0 = springChat&amp; p1 = Erro ... gleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.15%2Fangular .min.js%3A18%3A179)(...)(匿名函数)@ angular.js:38(匿名函数)@ angular.js:4138r @ angular.js:323 g @ angular.js:323
答案 0 :(得分:0)
您可能忘记在index.html中包含该文件,或者您可能忘记将其包含在angular.module中。