0

I get the following error - angular.js:38Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.5.5/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope

I am trying to send a subscribe message, using pubnub, and incorporate an ng-notifications-bar for whether the subscribe message comes through successfully or not.

The code is as follows for the controller, the main parts which I am not sure about is the ngNotifications part, (showError, showSuccess, showWarning etc). You will see in the last part I've implemented an if else statement to specify when to apply a success notification and when to apply an error notification:

myApp.controller('NotificationsController',['$scope','$http','$rootScope','$location','token_service','PubNub','notifications', function($scope,$http,$rootScope,$location,token_service,PubNub,notifications){

  $scope.noti = [];
  function theCallback (x){
    $scope.$apply(function () {
      $rootScope.spinner = false;
      $scope.noti = x[0];
      console.log(x[0]);
    });
  }

  $scope.showError = function () {
    notifications.showError("Error, are you sure you've followed the correct process?");
  };
  $scope.showWarning = function () {
    notifications.showWarning('Having trouble, please try again.');
  };
  $scope.showSuccess = function () {
    notifications.showSuccess('Welcome to Chui!');
  };

  $rootScope.defaultInstance.history({channel:"chui_channel", count:100,callback:theCallback});

  $rootScope.spinner = true;
  $scope.keys='test_notifications'
}])
//myApp.controller('SignoutController',['$scope','$rootScope','$location','token_service','signout_service','$window',function($scope,$rootScope,$location,token_service,signout_service,$window){

myApp.run(function($scope,$rootScope,$location,$templateCache,$window,$cookies,$route,api_service,notifications) {

    $rootScope.defaultInstance = PUBNUB.init({
        publish_key: 'pub-c-5aace072-1c95-4d6c-8b95-28638c4bd6a2',
        subscribe_key: 'sub-c-0ac5d634-10aa-11e6-bbd9-02ee2ddab7fe'
    });



    var device = api_service.get_devices()
    device.then(function(response){
      $rootScope.devices = response;
      console.log(response);
      var deviceSerialList = new String
      for (var d in response){
        deviceSerialList = String(response[d].DeviceId) +',';
        var l = deviceSerialList.split(',').filter(Boolean);
        if (l.length == response.length){
          listen(deviceSerialList);
          console.log(deviceSerialList);
          $scope.showSuccess = function () {
             notifications.showSuccess('Welcome to Chui!');
         };
        }
        else{
          $scope.showError = function () {
            notifications.showError("Error, are you sure you've followed the correct process?");
          };
        }
      };
    });

    function listen(deviceList){
      $rootScope.defaultInstance.subscribe({
              channel : deviceList,
              message : function (message, envelope, channelOrGroup, time, channel) {
                alert(message)
                  console.log(
                      "Message Received." + "\n" +
                      "Channel or Group : " + JSON.stringify(channelOrGroup) + "\n" +
                      "Channel : " + JSON.stringify(channel) + "\n" +
                      "Message : " + JSON.stringify(message) + "\n" +
                      "Time : " + time + "\n" +
                      "Raw Envelope : " + JSON.stringify(envelope)
                  )
              },
      });
    };

In the html page I have included the following: and it already has

2
  • Hi Will. Welcome to StackOverflow! I recommend changing the title of your question to a description of your problem, not the error you're encountering. Also, if you have some additional information, you better edit your answer than add a comment. Commented Jun 2, 2016 at 20:00
  • Ok thanks for the info...much appreciated! Will do so... Commented Jun 2, 2016 at 20:02

1 Answer 1

0

myApp.run(function($scope - this is the cause. .run doesn't take a $scope injection - remove it.

Sign up to request clarification or add additional context in comments.

3 Comments

When I remove it the console tells me there is a reference error: $scope is not defined at main.js?v=1020:236 at angular.js:15961 at n.$eval (angular.js:17229) at n.$digest (angular.js:17045) at n.$apply (angular.js:17337) at l (angular.js:11572) at H (angular.js:11778) at XMLHttpRequest.u.onload (angular.js:11711) Any ideas?
@WillBodansky -- You reference $scope inside that function - remove that as well
thanks a lot for the help. I replaced $scope with $rootScope within the function and it's fixed the error arriving to the console. I'm still not getting the desired functionality, and so I will investigate and try and find the solution. If I'm still at a loss I might come back here for more help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.