【发布时间】:2021-08-31 13:01:19
【问题描述】:
我正在尝试传递从用户的下拉列表中选择的数据值。我有一个父组件 (app.component.html) 和子组件 (hello.component.html & hello.component.ts)。
根据选择的选项值,我想将值从父组件传递给子组件。到目前为止,我认为我所做的一切都是正确的。我可以知道为什么它不起作用吗?目前,浏览器提示“编译失败”。
app.component.html
<h2> Course Details </h2>
Select a course to view
<select #course (change)="name = course.value">
<option value="Node JS">Node JS</option>
<option value="Typescript">Typescript</option>
<option value="Angular">Angular</option>
<option value="React JS">React JS</option>
</select><br/><br/>
<app-hello [cName]="name"></app-hello>
<router-outlet></router-outlet>
hello.component.ts
import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-hello',
templateUrl: './hello.component.html', // templateUrl property is used to bind external template file with the component
styleUrls: ['./hello.component.css']
})
export class HelloComponent implements OnInit {
courses = [
{ courseId: 1, courseName: "Node JS" },
{ courseId: 2, courseName: "Typescript" },
{ courseId: 3, courseName: "Angular" },
{ courseId: 4, courseName: "React JS" }
];
@Input()
cName: any;
constructor() {
}
ngOnInit(): void {
}
}
hello.component.html
<table border="1" *ngIf="cName">
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let c of courses">
<td *ngIf="c.courseName == cName">{{c.courseId}}</td>
<td *ngIf="c.courseName == cName">{{c.courseName}}</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: javascript angular typescript