需要在 app.module.ts 文件中引入 BrowserModule 和 FormsModule 模块,以便 Angular 能够识别这些指令和属性。示例代码如下:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
除此之外,还需要在组件的 ts 文件中按需引入需要使用的指令和属性,例如:
import { Component } from '@angular/core';
import { NgIf } from '@angular/common';
@Component({
selector: 'app-root',
template: `
`
})
export class AppComponent {
showMessage = true;
onClick() {
console.log('Clicked!');
}
}