gpt4 book ai didi

angular - 在 angular4 中带有嵌套组件的 Bootstrap 表

转载 作者:行者123 更新时间:2023-12-04 03:07:12 26 4
gpt4 key购买 nike

我是 angular4 的初学者,我正在尝试使用嵌套组件构建 Bootstrap 表,子组件显示在单行中。但是表格显示不正确。请看下图

snapshot

父组件

<table class="table table-dark">
<thead>
<tr>
<th>Title</th>
<th>Done</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<app-child *ngFor="let todo of this.todos " [todo]="todo" (eventRemove)="remove(todo)"></app-child>
</tbody>
</table>
<hr>
<div>
<form #f="ngForm" (ngSubmit)="formSubmit(f.value)">
<label for="title"></label>
<input type="text" name="title" id="title" [(ngModel)]="this.todo.title">
<button type="submit">Add</button>
</form>
</div>



import { Todo } from '../todo';
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

public todos: Todo[] = [];
public todo: Todo = { title: "", done: false };
formSubmit(value) {
var todo = { title: value.title, done: false };
this.todos.push(todo);
}
constructor() {


}

ngOnInit() {
this.todos.push({ title: "clean your room", done: false });
this.todos.push({ title: "clean your desk", done: true });

}
remove(t) {
let index = this.todos.indexOf(t);
this.todos.splice(index, 1);
console.log("removed", t);
}

}

子组件

<tr>
<td>
{{todo.title}}
</td>
<td>
<input type="checkbox" [checked]="todo.done">
</td>
<td>
<button class="btn btn-success btn-sm" (click)="onRemove(todo)">Remove</button>
</td>
</tr>

从'../todo'导入{Todo};从 '@angular/core' 导入 { Component, Input,Output, OnInit ,EventEmitter};

@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})

export class ChildComponent implements OnInit {

@Input()
todo: Todo;
@Output()
eventRemove = new EventEmitter<Todo>();
constructor() { }

ngOnInit() {
}
onRemove(data) {

this.eventRemove.emit(data);
}
}

最佳答案

如果你想使用一个子组件来显示表格,只需调用这个子组件一次,然后将 ngFor 循环放在那个子组件中,传递 'this. todos 的变量。

但是如果你只在一个地方显示表格,使用子项可能没有多大意义,你可以在 <tbody> 中循环 'this.todos'就是这样。

关于angular - 在 angular4 中带有嵌套组件的 Bootstrap 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47830068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com