【发布时间】:2019-02-20 12:57:59
【问题描述】:
我有一个 Angular 页面,它使用一个组件来显示它的一些属性。但是组件的属性不会显示在页面上。代码如下:
HTML 页面 (testPage.component.html)
<p>title: {{title}}</p>
<p>another title: {{anotherTitle}}</p>
TypeScript (testPage.component.ts)
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-testPage',
templateUrl: './testPage.component.html',
styleUrls: ['./testPage.component.css']
})
export class TestPageComponent implements OnInit {
title: string;
anotherTitle = "This is another title";
setTitle(): void {
this.title = "This is title!!";
console.log('This is title', this.title);
}
}
var testPageComponent = new TestPageComponent();
//newBudgetComponent.title = "some title here!!"; //Also not working
testPageComponent.setTitle();
在页面中,anotherTitle 填充得很好,但 title 没有填充。
函数 setTitle 记录标题,但 Angular 页面不显示该值。
这是页面的外观:
如何在组件外设置组件属性?
【问题讨论】:
-
你到底想在这里实现什么?
-
请分享用例。可能有几种方法可以做到这一点,但归根结底,您为什么要尝试从应用程序外部设置值?
标签: angular typescript components