点击添加按钮增加一个组件

父组件html:

<button class="button" (click)="add()">add</button>
  <button class="button" (click)="save()">alert</button>

  <div *ngFor="let article of all;let idx=index">
    <app-article [attr.data-index]="idx" [article]="article"></app-article>
  </div>

父组件ts:

articles: Article[];
  all: any[] = [{}];
add() {
    this.all.push({});
  }

子组件html:

 <div class="value">
      <div class="label"  >标题 </div> 
      <input type="text" [(ngModel)]="article.title" id="title">
      <input type="text" [(ngModel)]="article.link">
    </div>

子组件ts:

 @Input() article: Article;

article.model.ts

export class Article {
    votes: number;
    title: string;
    link: string;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 组件(Component)是Vue.js最核心的功能,也是整个架构设计最精彩的地方,当然也是最难掌握的。...
    六个周阅读 10,926评论 0 32
  • 什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装...
    youins阅读 13,159评论 0 13
  • 此文基于官方文档,里面部分例子有改动,加上了一些自己的理解 什么是组件? 组件(Component)是 Vue.j...
    陆志均阅读 9,236评论 5 14
  • <1>输入属性 定义:组件的输入属性,是指被@input装饰器注解的属性,用来从父组件接收数据 实例1.新建ord...
    mumumuu阅读 3,619评论 0 1
  • 一、什么是组件(Component)? 组件(Component)是Vue.js最强大的功能之一。组件可以扩展HT...
    廖马儿阅读 24,452评论 1 4