我想通过按下按钮"更多"来从json文件中获取整篇文章文本。 当我按"更多"按钮我收到错误" EXCEPTION:在ArticlesService"上找不到指令注释。
article.ts
import {Component} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {MATERIAL_DIRECTIVES} from 'ng2-material/all';
import { Router } from 'angular2/router';
import {ArticlesService} from './article-service';
import {ArticlesDetailComponent} from './article-detail';
@Component({
selector: 'articles',
providers: [ArticlesService,HTTP_PROVIDERS],
template: `
<md-content class="md-padding" layout="row" layout-wrap layout-align="center start">
<img src="{{article.article_img}}" class="md-card-image" alt="Grass">
<md-card-title>
<md-card-title-text>
<span class="md-headline">{{article.article_title}}</span>
</md-card-title-text>
</md-card-title>
<md-card-content>
{{article.article_content}}
</md-card-content>
<md-card-actions layout="row" layout-align="end center">
<button md-button (click)="gotoDetail()">More</button>
</md-card-actions>
</md-card>
</div>
</md-content>`,
directives: [MATERIAL_DIRECTIVES,ArticlesDetailComponent]
})
export class ArticlesComponent {
constructor(private _articles:ArticlesService,private _router:Router) {}
public articles;
private errorMessage;
ngOnInit() { this.getArticles();}
getArticles() {
this._articles.getArticles()
.subscribe(articles =>
this.articles = articles,
error => this.errorMessage = <any>error);
}
gotoDetail() {
this._router.navigate(['ArticlesDetail', { id: this.articles.id }]);
}
}
和articles-detail.ts
import {Component,OnInit} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {MATERIAL_DIRECTIVES} from 'ng2-material/all';
import {ArticlesService} from './article-service';
import { RouteParams } from 'angular2/router';
@Component({
selector: 'articles-detail',
providers: [ArticlesService,HTTP_PROVIDERS],
template: `
<p>Detail</p>
`,
directives: [MATERIAL_DIRECTIVES,ArticlesService]
})
export class ArticlesDetailComponent implements OnInit {
constructor(private _articles:ArticlesService,private _routeParams: RouteParams) {}
public article;
ngOnInit() {this.getArticle();}
getArticle() {
let id = +this._routeParams.get('id');
console.log(id);
this._articles.getArticle(id)
.then(article => this.article = article);
}
goBack() {
window.history.back();
}
}
答案 0 :(得分:2)
您将服务设置为directives
组件的ArticlesDetailComponent
属性:
@Component({
selector: 'articles-detail',
providers: [ArticlesService,HTTP_PROVIDERS],
template: `
<p>Detail</p>
`,
directives: [MATERIAL_DIRECTIVES,ArticlesService] // <-----------
})
export class ArticlesDetailComponent {
(...)
}
但它不是一个组件。这就是您收到错误消息的原因......