erp
erp

Reputation: 3014

Possible strict violation one place but not another

I know there are a few possible strict violation questions on here, but I haven't been able to come to a result based on them. I am trying to use var vm = this in a controller in one class and it's throwing this error, but I have done the same thing in other controllers more than once with no possible strict violation errors.

So here is the first js file. it's an angular directive with the controller in the same file. The error is coming from the var vm = this in the controller below.

angular.module('app.monitor').directive('scModelLegacyViewer',
      scModelLegacyViewer);

  function scModelLegacyViewer() {

    return {
      restrict : 'E',
      templateUrl : 'app/monitor/monitor.html',
      scope : {
        config : '=',
        register : '&?',
        data : '=?',
        allowEditingSwitch : '=?'
      },
      controller: scModelLegacyViewerController,
      controllerAs: 'vm'
    };
  }

  scModelLegacyViewerController.$inject = [ '$q', '$scope', '$timeout', 'config',
                                            'logger', 'ProjectService', 'ModelService', 'InstanceService',
                                            'BayesianService'];

  function scModelLegacyViewerController($q, $scope, $timeout,
      config, logger, ProjectService,
      ModelService, InstanceService, BayesianService) {
      var vm = this;  // HERE IS THE ERROR LINE
      vm.modelInstanceChannel = 'MODEL_INSTANCE';
      vm.saveAll = saveAll;
      ...

Another file where this works perfectly is this for example, which throws no errors:

 angular
    .module('app.model')
    .controller('ModelController', ModelController);

  //scInitialConfig added in model.route.js
  ModelController.$inject = ['$document', '$interval', '$scope', '$stateParams', 'logger',
      'modelService', 'scInitialConfig'];
  /* @ngInject */
  function ModelController($document, $interval, $scope, $stateParams, logger,
      modelService, scInitialConfig) {
    var vm = this;
    var data = null;
    vm.instance = 'default';
    vm.title = 'Model Viewer';
    ...

The only difference that I can come up with is that @ the top of the first file I am declaring the directive but in the 2nd file it is just the controller. Unfortunately I'm not an angular expert, so I don't know if that is an issue, but just thinking this might be where the error is coming from?

Upvotes: 0

Views: 396

Answers (2)

Andrey Goncharov
Andrey Goncharov

Reputation: 111

Rename function scModelLegacyViewerController to ScModelLegacyViewerController. JsHint allows assigning this to a variable only if the name starts with a capital letter. So it's going to be

angular.module('app.monitor').directive('scModelLegacyViewer',
      scModelLegacyViewer);

  function scModelLegacyViewer() {

    return {
      restrict : 'E',
      templateUrl : 'app/monitor/monitor.html',
      scope : {
        config : '=',
        register : '&?',
        data : '=?',
        allowEditingSwitch : '=?'
      },
      controller: ScModelLegacyViewerController,
      controllerAs: 'vm'
    };
  }

  function  ScModelLegacyViewerController() {/*code*/}

Upvotes: 3

rtbm
rtbm

Reputation: 536

Maybe just use this to assign properties?

I mean this.foo = 'bar';

Upvotes: 0

Related Questions