javascript - AngularJS + Signalr not connecting to hub when controller is in a ng-include -
javascript - AngularJS + Signalr not connecting to hub when controller is in a ng-include -
i experiencing unusual problem connecting hub controller when controller "inside" 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);     }); }   homecoming {     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 vm">     server message {{vm.message}} </div> <div data-ng-controller="logincontroller vm">     server message 2: {{vm.message}} </div> <div data-ng-include="'app/layout/shell.html'"></div>     and shell.html:
<div data-ng-controller="mycontroller vm">     server message {{vm.message}} </div>    debugging service initialize triggered 3 times, serverside onconnect triggered twice. posting message server controllers in index.html updated, 1 in shell not. if remove controllers index.html 1 initialize , 0 onconnect serverside.
however if drop using service , set next code within controller 3 connections , update 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 else seen before or can explain why controller within ng-incude not working using service?
i think might have 2 different issues:
you should define.on handler on signalr proxy before calling .start(), should help solving missing invocation of onconnected server side (sounds weird, know, try) nginclude introduces isolated scope,  hence have 1 more level of scopes there, introduce issue. moreover, in order send events parent  kid scopes, doing, should using $broadcast (check here)   both first-impression things see , might want try, did not test though...
 javascript asp.net angularjs signalr signalr-hub 
 
Comments
Post a Comment