私は最初のAngular2プロジェクトでフォームを作成していますが、*ngIf
でテンプレートのdivを隠すことができません。Angular2 * ngIfテンプレート内のDivの内容を隠す
ここに私のコンポーネントファイルです:
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Address } from './signup'
@Component({
selector: 'address-area',
styles: [],
template: `
<form name='addressForm' novalidate>
<label> Get The Report via Standard Mail:</label>
<input name='needAddress' class='checkbox' type='checkbox' [(ngModel)]='address.need' />
<div *ngIf='isNeeded()'>
<input name='add1' class='text' type='text' placeholder='Address Line 1' [(ngModel)]='address.add1' />
<input name='add2' class='text' type='text' placeholder='Address Line 2' [(ngModel)]='address.add2' />
<input name='city' class='text' type='text' placeholder='City' [(ngModel)]='address.city' />
<input name='state' class='text' type='text' placeholder='State' [(ngModel)]='address.state' />
<input name='zip' class='text' type='text' placeholder='Zip' [(ngModel)]='address.zip' />
<input name='country' class='text' type='text' placeholder='Country' [(ngModel)]='address.country' />
</div>
</form>
`
})
export class AddressComponent {
address: Address = new Address(false);
isNeeded = function(){
this.address.need == true;
}
}
とちょうど参考のために、ここでのクラスオブジェクトです。
export class Address{
need: boolean;
add1: string;
add2: string;
city: string;
state: string;
zip: string;
country: string;
constructor(public n: boolean){
this.need = n;
}
}
を変更
div
return this.address.need; '。 – strまた、メソッドを削除して、テンプレートから 'address.need'を直接使用してください。フィールドを 'needed'にリネームし、それをAddressクラスから移動すると、IMHOも明確になります。必要な場合は住所を、そうでない場合は住所があります。必要のない住所を持つことは私には奇妙に見えます。 –
oh derp ......:/ –