【发布时间】:2017-11-15 16:55:29
【问题描述】:
<body>
<app-root data="myData"></app-root>
</body>
我需要检索myData,我将手动添加到index.html 并将其用于组件。
如何检索这些数据?
谢谢。
【问题讨论】:
-
如果是手动添加,为什么不直接手动添加到组件中呢?
标签: angular
<body>
<app-root data="myData"></app-root>
</body>
我需要检索myData,我将手动添加到index.html 并将其用于组件。
如何检索这些数据?
谢谢。
【问题讨论】:
标签: angular
我相信这个答案有你所需要的:
https://stackoverflow.com/a/43544999/3581932
使用@Attribute(对于 Angular 4)
index.html:
<my-app myAttribute="myAttributeValue">
<div>Loading...</div>
</my-app>
app.component.ts :
@Component({
selector: 'my-app',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css']
})
export class AppComponent {
constructor(private elementRef:ElementRef) {
let myAttribute = this.elementRef.nativeElement.getAttribute('myAttribute');
}
}
[直接从上面的帖子复制]
【讨论】: