Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS with SignalR

I am playing with Angular and SignalR, I have tried to create a service which will act as a manager.

dashboard.factory('notificationsHub', function ($scope) {
  var connection;
  var proxy;

  var initialize = function () {
    connection = $.hubConnection();
    proxy = connection.createHubProxy('notification');

    proxy.on('numberOfIncidents', function (numOfIncident) {
      console.log(numOfIncident);
      $scope.$emit('numberOfIncidents', numOfIncident);
    });

    connection.start()
      .done(function() {
        console.log('Connected');
      })
     .fail(function() { console.log('Failed to connect Connected'); });
  };

  return {
    initialize: initialize
  };
});

however I get the error Error: Unknown provider: $scopeProvider <- $scope <- notificationsHub.

How can I use pubsub to pass all the notifications to the controllers? jQuery maybe?

like image 975
li-raz Avatar asked Nov 18 '13 20:11

li-raz


1 Answers

$scope does not exist in this context as that's something injected when a controller is created and a new child scope is made. However, $rootScope is available at the time you need.

Also, be aware $emit() goes upward and your controller scopes wont see it. You would either need to switch to $broadcast() so the event goes downwards or inject $rootScope as well to the controllers you want to be able to subscribe to 'numberOfIncidents'

Check out the angular docs and a useful wiki on scopes.

like image 104
johlrich Avatar answered Sep 23 '22 13:09

johlrich