実現したいこと
数日前からAngularの勉強を始めました。
Angularにて、FormsModuleを追加してフォームを使いたいです。
発生している問題・分からないこと
HelloComponent.htmlに
html
1<input type="text" [(ngModel)]="text1" />
を実装したところ、以下のようなエラーが出ています。
エラーメッセージ
error
1Can't bind to 'ngModel' since it isn't a known property of 'input'.ngtsc(-998002) 2hello.component.ts(12, 21): Error occurs in the template of component HelloComponent.
該当のソースコード
HelloComponent.html
1<div id="body"> 2 <h1>{{title}}</h1> 3 <p>{{message}}</p> 4 <p>TYPE: {{text1}}</p> 5 <input type="text" [(ngModel)]="text1" /> 6</div>
main.ts
1import { bootstrapApplication } from '@angular/platform-browser'; 2import { appConfig } from './app/app.config'; 3import { AppComponent } from './app/app.component'; 4import { HelloComponent } from './app/hello/hello.component'; 5import { FormsModule } from '@angular/forms'; 6 7bootstrapApplication(HelloComponent, appConfig) 8 .catch((err) => console.error(err));
HelloComponent.ts
1import { NgClass, NgFor, NgIf } from '@angular/common'; 2import { Component } from '@angular/core'; 3 4@Component({ 5 selector: 'app-hello', 6 standalone: true, 7 imports: [NgFor,NgIf, NgClass], 8 templateUrl: './hello.component.html', 9 styleUrl: './hello.component.css' 10}) 11 12export class HelloComponent { 13 title:string = ""; 14 message:string = ""; 15 text1:string = ""; 16 17 ngOnInit() { 18 this.title = "Hello-app!"; 19 this.message = "ngModel1を使う"; 20 this.text1 = ""; 21 } 22}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
「Angular 超入門(掌田津耶乃, 秀和システム)」という参考書を元にFomsModuleを使おうとしているのですが、参考書のバージョンが古く、私の方には「app.module.ts」がないため、追加方法がわかりませんでした。
試行錯誤してみましたが、エラーを解消できないため、ご教授願います。
補足
Angularのバージョン:v17.3.0

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/08/16 01:47