TL;DR:,我想在html模板中将一个变量从一个组件传递到另一个组件。这样的东西在docs中有描述:<my-comment (blogId)="blog.id"></my-comment>和trows没有错误。但是如何在其他组件中“缓存”这个变量呢?我想把博客Id作为变量传递给API。
长版本:,我制作了一个带有父-子组件和一个单独组件的Angular2。它们是:博客列表(帖子列表),一篇文章和评论。
我尝试将博客id检索从blog API传递给comments组件并放到它的API中。
所以我有: comments.service
(...)
@Injectable()
export class CommentsService {
constructor(private http: Http) {}
private headers = new Headers({'Content-Type': 'application/json'});
create(body: string, author: string, date: string, post: any): Promise<Comments> {
return this.http
.post(API_ENDPOINT, JSON.stringify({body: body, name: author, date: date, postId: post}), {headers: this.headers})
.toPromise()
.then(this.extractData);
}
(...)
}comments.component,这里有一个Ts错误:提供的参数与调用目标的任何签名不匹配。在这一行上:(但是应用程序和API工作得很好)。
This.commentsService.create(身体,名字,日期,postId)
import {Component, ViewEncapsulation, OnInit, OnDestroy, NgModule, Input} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/toPromise';
import {CommentsService} from "./comments.service";
import {Comments} from "./comments.model";
import {PostComponent} from '../blog/post.component';
import { BlogService } from '../blog/blog.service';
@Component({
selector: 'my-comment',
templateUrl: './comments.html',
styleUrls: ['./comments.scss'],
encapsulation: ViewEncapsulation.None,
providers: [CommentsService]
})
@NgModule({
declarations: [PostComponent]
})
export class CommentsComponent implements OnInit, OnDestroy {
private postId: Comments;
private body: Comments;
private name: Comments;
private date: any = new Date().toLocaleString();
private postComponent: any = PostComponent;
private blog: any = BlogService;
constructor(
private route: ActivatedRoute,
private commentsService: CommentsService
) {
}
ngOnInit(): void {
let id = +this.route.snapshot.params['id'];
}
ngOnDestroy(): void {}
add(body: string, name: string, date: string, postId: number): void {
body = body.trim();
name = name.trim();
if (!body || !postId || !name) { return; }
this.commentsService.create(body, name, date, postId);
}
}Comments.html模板,我希望有一个博客Id变量。
<div ngclass="form" class="form">
<!--<input #name />-->
<p ngClass="form__title">We are thrilled to hear your opinion:</p>
<input #body ngClass="form__body" placeholder="Put your text here" />
<input #name placeholder="Your name" />
<button class="btn btn-1 btn-1d" (click)="add(body.value, name.value, date, ????blogPostID????); body.value=''; name.value='';">
Add
</button>
</div>Blog Post使用Id从API中检索,并在其父组件中通过*ngFor进行迭代。在post.component中,我提供了一个注释服务,因此这个模板对于一个帖子(post.html)非常有效:
<article *ngIf="blog" class="post">
<header>
<h2>{{ blog.id }}\ {{ blog?.title }}</h2>
</header>
<section class="article__content">
{{ blog?.text }}
<p class="author"> {{ blog?.name }}</p>
</section>
<footer>
<a [routerLink]="['/blog']" routerLinkActive="active" class="btn btn-1 btn-1e">Return</a>
</footer>
</article>
<section *ngIf="comments" class="comment">
<h2>Comments:</h2>
<main>
<div *ngFor="let comment of comments">
<div *ngIf="comment.postId == blog.id">
<p>{{ comment.body }}</p>
<p>{{ comment.postId }}</p>
</div>
</div>
</main>
</section>
<my-comment></my-comment>但是我想提供一个从单个post (*ngFor迭代组件)到某种方式的blog.id。
发布于 2017-01-04 09:24:55
对我的问题的回答很简单,但我花了一些时间把所有的东西都整理好了。
要将变量传递给我在父模板中使用的另一个组件: Where是其他组件,blog.id是要传递给的变量。
<my-comment [blogId]="blog.id"></my-comment>在注释组件中,我添加了import:
export class CommentsComponent implements OnInit, OnDestroy {
@Input() blogId:number;
...然后,我可以在注释模板中使用blogId变量,它是post组件中blog.id的精确副本。
https://stackoverflow.com/questions/41262727
复制相似问题