0

这是一个不好的称号。我意识到。那是因为我不完全确定如何提出这个问题。我有两个基本相同的课程,表现只是稍微不同,相应的状态配置controllerAs: 'vm'为他们每个人的行为也不同,一个令人困惑的“这种方法可以是静态的”,在其中一人从Webstorm警告,而不是其他。角与ES6控制器出现异常行为

的index.html

<div ui-view="main"></div> 
<div ui-view="portfolio"></div> 

app.js

// this file contains only the module setter with all the 
// dependencies, as well as the $stateProvider config and 
// any actions that occur when the app runs 

'use strict'; 

angular.module('necApp', ['dep1', 'dep2', 'etc']) 

    .config(['$urlRouterProvider', '$locationProvider', '$animateProvider', Config]) 
    .run(['$rootScope', '$state', Run]); 

function Config(params) { /* do stuff */ } 
function Run(params) { /* do stuff */ } 

main.js

use strict'; 

import { MainController } from './main.controller'; 

angular.module('myApp') 
    .controller('MainController', MainController) 
    .config(['$stateProvider', Config]); 

function Config($stateProvider) 
{ 
    $stateProvider 

    .state('main', 
    { 
     url: '/', 
     views: { 
      'main': { 
       templateUrl: 'app/main/main.html', 
       // OF NOTE: I have access to the controller as 'vm' in the view 
       // regardless of whether I include the next two lines 
       controller: MainController, 
       controllerAs: 'vm' 
      } 
     } 

    }); 
} 

main.html中

<!-- 
    again, this expression is evaluated whether or not I include 
    the `controller` and `controllerAs` properties in my $state config 
--> 
<h1> {{ vm.result }} </h1> 

main.controller.js

// OF NOTE: when I DO include the `controller` property in the $state config 
// for the main route, this controller is registered and instantiated twice 
'use strict'; 

export class MainController 
{ 
    constructor($http) 
    { 
     /* @ngInject */ 
     angular.extend(this, { 
      $http: $http, 
      result: '' 
     }); 

     this.Init(); 
    } 

    Init() 
    { 
     this.$http.get('/endpoint').then(res => 
     { 
      this.result = res.data; 
     }); 
    } 
} 

portfolio.js

use strict'; 

import { PortfolioController } from './portfolio.controller'; 

angular.module('necApp') 
    .controller('PortfolioController', PortfolioController) 
    .config(['$stateProvider', Config]); 

function Config($stateProvider) 
{ 
    $stateProvider 

    .state('portfolio', 
    { 
     url: '/portfolio', 
     views: { 
      'portfolio': { 
       templateUrl: 'app/portfolio/views/portfolio.html', 
       // OF NOTE: I do NOT have access to the controller as 'vm' 
       // in the view in this case without the next two lines 
       controller: PortfolioController, 
       controllerAs: 'vm' 
      } 
     } 
    }); 
} 

portfolio.html

<!-- this is NOT evaluated without the `controller` and `controllerAs` properties in the $state config --> 
<h1> {{ someExpression }} </h1> 

portfolio.controller.js

'use strict'; 

export class PortfolioController 
{ 
    constructor() 
    { 
     angular.extend(this, { 

      someExpression: 'Testing . . .' 
     }); 

     this.Init(); 
    } 

    // OF NOTE: Webstorm warns me that this method can be static, even though 
    // 1) that breaks it and 2) I do NOT get that warning in MainController 
    Init() 
    { 
     // works as expected 
     console.log('initializing PortfolioController.'); 
    } 
} 

像往常一样,我非常期待着您的想法和意见。

+0

我想每一个没有“这个”在它的身上会得到警告的方法。 – estus

回答

1

好了,之前别人浪费了这个自己宝贵的时间,原来我只是愚蠢。或者健忘。我发现一个遗忘和未使用的指令,我写的是因为某种原因使用MainController作为'vm'。吉兹。

虽然:我仍然有Webstorm警告我PortfolioController.Init()可以是静态的,但我没有得到MainController.Init()的警告。所以这仍然是一个谜,我猜。

+0

我发誓,在我发布之前,我在项目范围内搜索了“MainController”。我觉得我只是醉了 - 拨打了Stackoverflow。 – MyCompassSpins