[英]*ngFor with two way binding in IONIC 2
I have a page with a button, that when is pressed, it shows a modal that contains a checkbox list.我有一个带有按钮的页面,当按下该按钮时,它会显示一个包含复选框列表的模式。 When this modal is closed, it returns the items that was checked, and I want to make a new badge for every returned item in the original page.
当此模式关闭时,它会返回已检查的项目,我想为原始页面中的每个返回项目制作一个新徽章。
I was trying with *ngFor, but is not working, because the "tags" was loaded after this page was loaded.我正在尝试使用 *ngFor,但不起作用,因为在加载此页面后加载了“标签”。
So my question is, how can I make a "*ngFor" that automatically refresh the interface when the model changes?所以我的问题是,如何制作一个“*ngFor”,在模型更改时自动刷新界面? Something like two way binding.
类似于双向绑定的东西。
This is my html code:这是我的 html 代码:
<button ion-item detail-push (click)="showTagsDialog()">
<div style="float:left; width:100%; display:block; margin-left:-10px;">
<button ion-button style="float:left; font-size:1.2rem !important; color:#999; float:left; display:block; text-transform: capitalize;" clear>
Tags
</button>
</div>
<ion-badge *ngFor="let tag of tags">{{tag.name}}</ion-badge>
</button>
EDIT: I return the selected items when modal page is closed, using observable pattern, like this:编辑:当模态页面关闭时,我使用可观察模式返回所选项目,如下所示:
dismiss() {
this.events.publish(EventIndex.onTagsModalClose, this.dataset.filter(x => { return x.isSelected==true }));
this.viewCtrl.dismiss();
}
And I recibe like this:我是这样的:
private onTagsModalClose(args: any) {
this.tags = args;
this.hasSelectedTags = (<any[]>args).length > 0;
console.log(this.tags);
}
In the console, it shows the items correctly在控制台中,它正确显示了项目
Try this:尝试这个:
In your class declare tags as a BehaviorSubject在您的课程中将标签声明为 BehaviorSubject
tags : BehaviorSubject<any> = new BehaviorSubject([]);
When you recieve the new tags you must emit the new value当您收到新标签时,您必须发出新值
private onTagsModalClose(args: any) {
this.tags.next(args);
this.hasSelectedTags = (<any[]>args).length > 0;
console.log(args);
}
And in your template subcribe to it with the async pipe并在您的模板中使用异步管道订阅它
<button ion-item detail-push (click)="showTagsDialog()">
<div style="float:left; width:100%; display:block; margin-left:-10px;">
<button ion-button style="float:left; font-size:1.2rem !important; color:#999; float:left; display:block; text-transform: capitalize;" clear>
Tags
</button>
</div>
<ion-badge *ngFor="let tag of tags | async">{{tag?.name}}</ion-badge>
</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.