は、なぜあなたは、おかげであなたが直接、正常に動作し、あなたのclass App
//our root app component
import {Component, NgModule, VERSION} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<h1>Hello world</h1>
<p>Please select one option : </p>
<select [(ngModel)]="value">
<option [ngValue]="undefined">(undefined)</option>
<option *ngFor="let entry of entries" [ngValue]="entry.code">{{entry.label}}</option>
</select>
<p>Selected value : {{value}}</p>
`,
})
export class App {
value = undefined;
entries=[{code:"1", label:"abra"},{code:"2", label:"kadabra"}];
constructor() {
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
コード以下のよう
非常に簡単な解決策で
entries
配列を使用することができますgetEntries()
機能を使用しています!しかし、 'ngValue'と' value'の違いは何ですか?私は最初の '[ngValue] = "未定義"' 'に[値]あまりに' = "未定義" を変更する必要がありますか? – Dartz[ngValue]は、その要素のAngularバインディングを作成します。ただし、[値]は単純な属性バインディングです。属性バインディングは、すでに値にアクセスするためにngModelを使用できるため、行う必要があります。 – Ashvin777