【发布时间】:2017-12-16 02:52:40
【问题描述】:
我有两个组件,一个是 Post:
import {Component} from '@angular/core';
@Component({
selector: 'post',
template: `
<h1>{{title}}</h1>
<p>{{postText}}</p>
<ul>
<li *ngFor="let postData of posts">
<news-feed></news-feed>
</li>
</ul>
`
})
export class Post {
title : string;
postText : string;
posts = [{title:"Post1",postText:"Wow greate post"}, {title:"Post1",postText:"Wow greate post"}]
constructor(title:string, postText:string) {
this.title = title;
this.postText = postText;
}
}
另一个是新闻源:
import {Component} from '@angular/core';
@Component({
selector: 'news-feed',
template: `
<h1>News Feed</h1>
<div class="radios">
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="radio" value="success" [(ngModel)]="commandResult.type (change)="getCommandResult()"> Succeeded
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
<input class="form-check-input" type="radio" value="fail [(ngModel)]="commandResult.type (change)="getCommandResult()"> Failed
</label>
</div>
</div>`
})
export class NewsFeed {
commandResult: any = {
type: 'fail'
};
constructor() {
}
getCommandResult() {}
}
我在帖子组件中重复新闻提要组件,当我更改其中一个重复组件中的单选按钮时,我看到它在所有重复组件中都发生了变化。由于我是 ang2 的新手,所以我可能以错误的方式处理这个问题。任何帮助表示赞赏。 In this link, I think I am facing similar issue, I know its angular1 but still the same issue.
【问题讨论】:
-
你需要了解父子组件交互。 Angular 文档是一个很好的来源
-
@Vega 你能告诉我我在这里做错了什么吗?我查看了文档,但找不到任何相关信息。
标签: javascript angular angular2-directives angular-components