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コントローラがまだ同じエラーが発生し、私は完全に困惑しています。
をあなたがどんな解決策を見つけましたか? – gmodrogan
残念ながら。私はng-injection注釈の使用を避けて終了しました。 – Suavelizard
私はrequire( './ path to file')を使ってこれを修正しました。たとえば、app.servicesモジュールをロードして別のファイルに定義された異なるサービス(ファクトリ)を持っている場合、app.servicesモジュールを定義するファイルでは、ファイルの最後にrequire( './ path to file ')、それは素晴らしい作品です – gmodrogan