【发布时间】:2019-09-02 10:28:08
【问题描述】:
我正在尝试使用来自 HTML 页面的 @Input 接收数据。
示例 - 根组件
Template : "<div><comp1 value='hello'</div>"
Selector : "app-root"
子组件
Template : "<input type='text' [value]='value'/>"
Selector : "comp1"
class{
@Input value:string;
}
index.html
<body>
<app-root></<app-root>
</body>
尝试 1: 结果:子组件显示 hello
尝试 2: index.html
<body>
<comp1 value='test'></comp1>
</body>
引导更改为 ChildComponent
结果:子组件值什么都不显示。
子 ts 文件
import { Component, OnInit, Input, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-arc-chart',
templateUrl: './arc-chart.component.html',
styleUrls: ['./arc-chart.component.css'],
encapsulation: ViewEncapsulation.Emulated
})
export class ArcChartComponent implements OnInit {
@Input() textValue:string = "123";
constructor() {
}
ngOnInit() {
debugger;
console.log(this.textValue);
}
}
child html file
<input type="text" value="{{textValue}}" />
应用 ts 文件
import {
Component
} from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
}
应用程序 html 文件
<div>
<app-arc-chart textValue="555"></app-arc-chart>
</div>
如果我更改 Index.html 中的属性值,我期待值更改。 <app-arc-chart textValue='hahaha'></app-arc-chart'>
【问题讨论】: