Si-N
Si-N

Reputation: 1505

Angular 1.5, strict mode annotation finding error on seemingly irrelevant line

I have the classic problem in Angular where when I minify my code I get the 'unknown provider' due to parameter mangling.

I read about the ng-strict-di directive (which I wish I would of known about months ago!) to enforce string equivalents being passed along with parameters.

This has helped me narrow down my problem as I now get this stack trace using my unminified code:

enter image description here

So the problem seems to be in a 3rd party library, I have commented where the error occurs:

ng.module('opentok', [])
  .factory('OT', function() {
    return OT;
  })
  .factory('OTSession', ['OT', '$rootScope',
    function(OT, $rootScope) {
      var OTSession = {
        streams: [],
        connections: [],
        publishers: [],
        init: function(apiKey, sessionId, token, cb) {
          this.session = OT.initSession(apiKey, sessionId);

          OTSession.session.on({
            sessionConnected: function() {
              OTSession.publishers.forEach(function(publisher) {
                OTSession.session.publish(publisher);
              });
            },
            streamCreated: function(event) {
              //ERROR OCCURS ON THE LINE BELOW
              $rootScope.$apply(function() {
                OTSession.streams.push(event.stream);
              });
            },
            streamDestroyed: function(event) {
              $rootScope.$apply(function() {
                OTSession.streams.splice(OTSession.streams.indexOf(event.stream), 1);
              });
            },
            sessionDisconnected: function() {
              $rootScope.$apply(function() {
                OTSession.streams.splice(0, OTSession.streams.length);
                OTSession.connections.splice(0, OTSession.connections.length);
              });
            },
            connectionCreated: function(event) {
              $rootScope.$apply(function() {
                OTSession.connections.push(event.connection);
              });
            },
            connectionDestroyed: function(event) {
              $rootScope.$apply(function() {
                OTSession.connections.splice(OTSession.connections.indexOf(event.connection), 1);
              });
            }
          });

          this.session.connect(token, function(err) {
            if (cb) cb(err, OTSession.session);
          });
          this.trigger('init');
        },
        addPublisher: function(publisher) {
          this.publishers.push(publisher);
          this.trigger('otPublisherAdded');
        }
      };
      OT.$.eventing(OTSession);
      return OTSession;
    }
  ])

Everything seems in order and the line where the error occurs doesn't seem to relevant to DI. Can you spot what is wrong?

UPDATE If I comment out the line OTSession.streams.push(event.stream); then the error goes away. This seems weird as all it's doing is pushing an object to an array.

Upvotes: 0

Views: 350

Answers (2)

Pramod_Para
Pramod_Para

Reputation: 681

Write your controller function in this notation.

module.controller('AppCtrl', ['$scope','$timeout', function 
 ($scope,$timeout) {

}]);

This should fix the issue.

Upvotes: 2

Ritesh Srivastava
Ritesh Srivastava

Reputation: 174

can you add the $rootScope inject to your controller so that while minify this should not get any issue, like XXXController.$inject = ['$rootScope'];

Upvotes: 0

Related Questions