简体   繁体   English

可能严格违反某个地方,但不能违反另一地方

[英]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. 我知道这里possible strict violation存在一些possible strict violation问题,但是我无法根据这些问题得出结果。 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. 我试图在一类的控制器中使用var vm = this ,并且抛出此错误,但是我已经在其他控制器中多次进行了相同的操作,并且没有possible strict violation错误。

So here is the first js file. 所以这是第一个js文件。 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. 错误来自下面的控制器中的var vm = this

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 . 我能想出的唯一区别是,@的第一个文件的顶部,我宣布了directive ,但在第2个文件,它仅仅是个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? 不幸的是,我不是专家,所以我不知道这是否是一个问题,只是想这可能是错误的出处?

Rename function scModelLegacyViewerController to ScModelLegacyViewerController. 将函数scModelLegacyViewerController重命名为ScModelLegacyViewerController。 JsHint allows assigning this to a variable only if the name starts with a capital letter. 仅当名称以大写字母开头时,JsHint才允许将其分配给变量。 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*/}

Maybe just use this to assign properties? 也许只是使用来分配属性?

I mean this.foo = 'bar'; 我的意思是this.foo = 'bar';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM