<div class="container">
<div class="row">
<form>
<div class="form-group">
<label for="exampleInputEmail1">X坐标</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="xcood" name="xcood">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Y坐标</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="ycood" name="ycood">
</div>
<button type="button" class="btn btn-default" (click)="additem()">添加</button>
</form>
</div>
<div class="row">
<data-table [array]="addArray"></data-table><!--导入dataTable组件,并且将父组件里面的form表单数据传递给子组件渲染-->
</div>
</div>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
addArray=[];
xcood: any;
ycood: any;
additem(){
this.xcood = (document.getElementsByName('xcood')[0] as HTMLInputElement).value;
this.ycood = (document.getElementsByName('ycood')[0] as HTMLInputElement).value;
this.addArray.push({
xcood:this.xcood,
ycood:this.ycood
})
}
}
<table class="table table-striped">
<thead>
<tr>
<th>X坐标</th>
<th>Y坐标</th>
<th>操作</th>
</tr>
</thead>
<tbody *ngIf="array.length!==0"><!--这里我们判断一下传递过来的数组是否为空,如果是空的话我们就没有必要渲染出来了-->
<tr *ngFor="let data of array">
<td>{{data.xcood}}</td>
<td>{{data.ycood}}</td>
<td><button type="button" class="btn btn-default" (click)="delete(data)">删除</button></td>
</tr>
</tbody>
</table>
import { Component,Input } from '@angular/core';
@Component({
selector: 'data-table',
templateUrl: './dataTable.component.html',
styleUrls: ['./dataTable.component.css']
})
export class DataTableComponent {
@Input() array:any;//接收父组件传递过来的addArray数组
index: number; //跟上面说的一样要先声明
delete(data){
this.index = this.array.indexOf(data);
if (this.index > -1) {
this.array.splice(this.index, 1);//跟上面说的一样在初始化的时候要用到this
}
}
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { DataTableComponent } from './dataTable/dataTable.component';
@NgModule({
declarations: [
AppComponent,
DataTableComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有