λ²κ·Έ μ€λͺ
κ΅¬μ± μμκ° μ ν μ μ΄ (μ ν 보νΈ)λ₯Ό λ°ννλ ν¨μλ₯Ό μ¬μ©νλ κ²½μ° νλκ° μκ³ μ½λκ° μ»΄νμΌ (Angular v8.2)μ μννλλΌλ νΈμ§κΈ°λ λλ½ λ νλμ κ΄λ ¨λ μ€λ₯λ₯Ό νμν©λλ€.
μ¬ννλ €λ©΄
*ngIf
μμ μ ν κ°λλ₯Ό μ¬μ©νμμμ€. μ€μ²© λ μμμμ κ²°μ λλ μΈν°νμ΄μ€μ νλλ₯Ό μ¬μ©ν΄λ³΄μμμ€.
// my-model.model.ts
type MyModel = (MyModelA | MyModelB) & { type: 'A' | 'B' };
interface MyModelA {
fieldA: any;
}
interface MyModelB {
fieldB: any;
}
// foo.component.ts
public model: MyModel;
public isModelA(model): model is MyModelA {
return model.type === 'A';
}
// foo.component.html
md5-1956e8def7f34e35cec69338ae3e15c2
μμλλ νλ
νΈμ§κΈ°μλ μ€λ₯κ° μμ΅λλ€.
μ μ¬νκ² νμ κ°λλ μΈμ΄ μλΉμ€μ 컨ν μ€νΈμ μ μ©λμ§ μμ΅λλ€ (μ΄κ²μ²λΌ μ»΄νμΌνλ λμ μ μ©λλ κ² κ°μ΅λλ€)
export class NgLetContext<T = any> {
// allows syntax "foo; let bar"
$implicit: T = null!;
// allows syntax "foo as bar"
ngLet: T = null!;
}
@Directive({
selector: '[ngLet]',
})
export class NgLetDirective<T> implements OnInit {
@Input()
set ngLet(value: T) {
this.context.$implicit = this.context.ngLet = value;
}
constructor(private containerRef: ViewContainerRef, private templateRef: TemplateRef<NgLetContext>) {}
private context = new NgLetContext<T>();
static ngTemplateContextGuard<T>(dir: NgLetDirective<T>, ctx: unknown): ctx is NgLetContext<T> {
return true;
}
ngOnInit() {
this.containerRef.createEmbeddedView(this.templateRef, this.context);
}
}
interface MyModel { baz: ExampleValue }
// foo.component.ts
public model: MyModel;
<div *ngLet="model.baz as item">
{{ item }}
</div>
μΈμ΄ μλΉμ€ μ ν (μ μ΄λ {{ }}
λ΄ item
μμ vscodeμ ν΄ν λ§μ°μ€ μ€λ²)μ any
μ»΄νμΌ ν λ μ νμ΄ μ¬λ°λ₯΄κ² ExampleValue
μ΄λ 곧 μΆμ λ Ivy-native μΈμ΄ μλΉμ€ (https://github.com/angular/vscode-ng-language-service/issues/335#issuecomment-693545000 μ°Έμ‘°)μμ μμ λ κ²μ΄μ§λ§, μ°λ¦¬κ° κ³ννκ³ μλ κ²μ μλλλ€. νμ¬ μΈμ΄ μλΉμ€ ꡬνμ κ³ μ ν μ νμΌλ‘ μΈν΄ κ·Έ μ μ μμ νμμμ€.
Ayazκ° μΈκΈνλ―μ΄μ΄ λ¬Έμ λ v11.1μμ μ΅νΈ μΈμΌλ‘ μΆμ λ μλ‘μ΄ μΈμ΄ μλΉμ€μμ ν΄κ²°λμμ΅λλ€. νμ₯ μ€μ μμ νμ±ν ν μ μμ΅λλ€.