เมื่อเขียนคอมโพเนนต์ Angular 2.0 หนึ่งจะกำหนดค่าเริ่มต้นสำหรับคุณสมบัติอย่างไร
ยกตัวอย่างเช่น - ฉันต้องการชุดfoo
ไป'bar'
โดยปริยาย 'baz'
แต่อาจมีผลผูกพันทันทีเพื่อแก้ไข สิ่งนี้มีบทบาทอย่างไรในตะขอเกี่ยวกับวงจรชีวิต?
@Component({
selector: 'foo-component'
})
export class FooComponent {
@Input()
foo: string = 'bar';
@Input()
zalgo: string;
ngOnChanges(changes){
console.log(this.foo);
console.log(changes.foo ? changes.foo.previousValue : undefined);
console.log(changes.foo ? changes.foo.currentValue : undefined);
}
}
ด้วยเทมเพลตต่อไปนี้นี่คือสิ่งที่ฉันคาดหวังว่าค่าจะเป็น ฉันผิดเหรอ?
<foo-component [foo] = 'baz'></foo-component>
เข้าสู่คอนโซล:
'baz'
'bar'
'baz'
<foo-component [zalgo] = 'released'></foo-component>
เข้าสู่คอนโซล:
'bar'
undefined
undefined