【发布时间】:2017-12-15 19:27:52
【问题描述】:
我正在尝试@Input 对象列表;这会导致 @Input 变量未定义。
什么有效: home.component.html:
<p>
<it-easy [mycount]="countItem" (result)="PrintResult($event)"></it-easy>
home works for {{countItem}}!
</p>
<p>
calculation result is: {{calcResult}}
</p>
还有home.component.ts中的代码
import { Component } from '@angular/core';
@Component({
selector: 'it-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent {
countItem: number;
calcResult: number;
constructor() {
this.countItem = 5;
this.calcResult = 0;
}
PrintResult($event) {
this.calcResult = +$event;
}
}
export class EasyComponent implements OnInit {
@Input('mycount') count = 0;
@Output('result') calculation = new EventEmitter<number>();
constructor() {
alert('count is ' + this.count);
}
所以我做了一些调整,尝试从同一个 Home.component.html 发送此处显示的对象列表。
myNodesList: {title: string, count: number}[];
this.myNodesList = [
{title: "first", count: 1},
{title: "second", count: 2},
{title: "third", count: 3},
];
<p>
<it-hard [node]="myNodesList"></it-hard>
</p>
})
export class HardComponent implements OnInit {
@Input() node: {title: string, count: number}[];
constructor() {
const length = this.node.length;
}
并且节点是“未定义的”.. 实际上在 node.length 处的构造函数中吹了。
有什么想法吗? (如果没有修复,我将不得不通过服务运行)。
提前致谢。
【问题讨论】:
标签: javascript html angular input