2016-08-15 13 views
3

Yeoman Angular-Fullstackジェネレータを使用して新しいコンポーネントを作成しましたが、私はそれにModalサービスを注入しようとしています。Angular-Fullstackで生成されたAngular 1.5コンポーネントへの依存性注入方法

Error: [$injector:strictdi] CampaignsComponent is not using explicit annotation and cannot be invoked in strict mode

そうのように、コントローラはなります:

'use strict'; 
const angular = require('angular'); 
const uiRouter = require('angular-ui-router'); 
import routes from './campaigns.routes'; 

export class CampaignsComponent { 
    /*@ngInject*/ 
    constructor(Modal) { 
    console.log('campaigns'); 
      this.confirmDelete = Modal.confirm.delete(); 

    } 
} 
// CampaignsComponent.$inject['Modal']; 
export default angular.module('myApp.campaigns', [uiRouter]) 
    .config(routes) 
    .component('campaigns', { 
    template: require('./campaigns.html'), 
    controller: CampaignsComponent, 
    controllerAs: 'campaignsCtrl' 
    }) 
    .name; 

ジェネレータは自動的にそのように見えるコンポーネントを足場

ng-annotateは、しかし、私はこれを取得サービスを注入するために使用されています

import angular from 'angular'; 
import uiRouter from 'angular-ui-router'; 
import routing from './main.routes'; 

export class MainController { 

    /*@ngInject*/ 
    constructor($http) { 
    this.$http = $http; 
    console.log('main') 
    } 

    $onInit() { 
    this.$http.get('/api/things') 
     .then(response => { 
     this.awesomeThings = response.data; 
     }); 
    } 

    addThing() { 
    if (this.newThing) { 
     this.$http.post('/api/things', { 
     name: this.newThing 
     }); 
     this.newThing = ''; 
    } 
    } 

    deleteThing(thing) { 
    this.$http.delete('/api/things/' + thing._id); 
    } 
} 

export default angular.module('myApp.main', [uiRouter]) 
    .config(routing) 
    .component('main', { 
    template: require('./main.html'), 
    controller: MainController 
    }) 
    .name; 

Campaiに$ httpを注入しようとしています。それは、この時点でダウンモーダルサービスにはありませんので、

/*@ngInject*/ 
    constructor($http) { 
    this.$http = $http; 
    console.log('main') 
    } 

のようなGNSコントローラがまだ同じエラーが発生し、私は完全に困惑しています。

+0

をあなたがどんな解決策を見つけましたか? – gmodrogan

+0

残念ながら。私はng-injection注釈の使用を避けて終了しました。 – Suavelizard

+1

私はrequire( './ path to file')を使ってこれを修正しました。たとえば、app.servicesモジュールをロードして別のファイルに定義された異なるサービス(ファクトリ)を持っている場合、app.servicesモジュールを定義するファイルでは、ファイルの最後にrequire( './ path to file ')、それは素晴らしい作品です – gmodrogan

答えて

0

$injectプロパティを使用してこのエラーを修正できます。基本的には、問題への解決策は、'$http'依存関係を注入することです。アプリケーションが有効に厳密ジモードで動作している間に、明示的に注釈されていない機能またはプロバイダを起動しようとしたとき

CampaignsComponent.$inject['$http']; 

このエラーが発生します。詳細はhereです。

0

は、あなたのメインコントローラクラスに次のメソッドを追加します。

$onInit(){} 
関連する問題