在Angular中,要显示变量的值,可以使用插值表达式或属性绑定。
{{ variable }}
下面是一个完整的Angular示例,演示了如何在模板中显示变量的值:
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
{{ variable }}
`,
styles: []
})
export class AppComponent {
variable = 'Hello Angular!';
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
在这个示例中,变量variable
被定义为Hello Angular!
。在模板中,{{ variable }}
使用插值表达式显示变量的值,[textContent]="variable"
使用属性绑定实现相同功能。
当你运行这个示例时,你会在浏览器中看到两个段落()元素,内容都是
Hello Angular!
。
上一篇:Angular不会识别新组件
下一篇:Angular不会显示mp4电影