I am experiencing a strange problem connecting to a hub from a controller when the controller is "inside" a ng-include.
Service:
angular.module('app').service('signalRSvc', function($, $rootScope) {
var proxy = null;
var initialize = function() {
var connection = $.hubConnection();
proxy = connection.createHubProxy('myHub');
connection.start();
proxy.on('srvMessage', function(data) {
$rootScope.$emit('newMessage', data);
});
}
return {
initialize: initialize
};
});
The Controller:
function MyController($scope, signalRSvc) {
signalRSvc.initialize();
var vm = this;
vm.message = '';
$scope.$parent.$on("newMessage", function (e, data) {
$scope.$apply(function () {
vm.mesage = data;
});
});
}
Index.html (stripped down):
<div data-ng-controller="MyController as vm">
Server message {{vm.message}}
</div>
<div data-ng-controller="LoginController as vm">
Server message 2: {{vm.message}}
</div>
<div data-ng-include="'app/layout/shell.html'"></div>
And Shell.html:
<div data-ng-controller="MyController as vm">
Server message {{vm.message}}
</div>
Debugging this my service initialize is triggered three times, but serverside OnConnect is only triggered twice. Posting a message to the server the controllers in Index.html is updated, but the one in shell is not. If I remove the controllers from Index.html I get 1 initialize and 0 OnConnect serverside.
However if I drop using a service and put following code inside the controller it get 3 connections and update all controllers:
var hub = $.hubConnection();
var proxy = hub.createHubProxy('myHub');
proxy.on('newMessage', function(data) {
$scope.$apply(function() {
vm.message = data;
});
});
hub.start().done(function () { });
Has anyone else seen this before or can explain why the controller inside ng-incude is not working using the service?