I'm baru di Sudut. Aku mulai Tur Pahlawan untuk belajar.
Jadi, saya menciptakan sebuah aplikasi.komponen
dengan dua arah
mengikat.
import { Component } from '@angular/core';
export class Hero {
id: number;
name: string;
}
@Component({
selector: 'app-root',
template: `
<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div><label>Name: </label>
<input [(ngModel)]="hero.name" placeholder="Name">
</div>
`,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
Berikut tutorial yang saya impor FormsModule dan ditambahkan ke deklarasi array. Kesalahan yang muncul pada langkah ini:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
FormsModule
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Berikut ini adalah kesalahan:
Uncaught Kesalahan: tak Terduga modul 'FormsModule' dinyatakan oleh modul 'AppModule'. Silahkan tambahkan @Pipa/@Direktif/@Komponen penjelasan.
FormsModule
harus ditambahkan pada impor array
tidak deklarasi array
.
BrowserModule
, FormsModule
, HttpModule
Komponen
, Pipa
, Arahan
lihat di bawah ini perubahan:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
Tambahkan FormsModule
Impor Array.
i.e
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
Atau hal ini dapat dilakukan tanpa menggunakan [(ngModel)]
dengan menggunakan
<input [value]='hero.name' (input)='hero.name=$event.target.value' placeholder="name">
bukan
<input [(ngModel)]="hero.name" placeholder="Name">