अपडेट करें
हम सिर्फ निर्देश बना सकते हैं *ngIfऔर इसे कॉल कर सकते हैं*ngVar
एनजी-var.directive.ts
@Directive({
selector: '[ngVar]',
})
export class VarDirective {
@Input()
set ngVar(context: any) {
this.context.$implicit = this.context.ngVar = context;
this.updateView();
}
context: any = {};
constructor(private vcRef: ViewContainerRef, private templateRef: TemplateRef<any>) {}
updateView() {
this.vcRef.clear();
this.vcRef.createEmbeddedView(this.templateRef, this.context);
}
}
इस *ngVarनिर्देश के साथ हम निम्नलिखित का उपयोग कर सकते हैं
<div *ngVar="false as variable">
<span>{{variable | json}}</span>
</div>
या
<div *ngVar="false; let variable">
<span>{{variable | json}}</span>
</div>
या
<div *ngVar="45 as variable">
<span>{{variable | json}}</span>
</div>
या
<div *ngVar="{ x: 4 } as variable">
<span>{{variable | json}}</span>
</div>
प्लंकर उदाहरण Angular4 ngVar
यह सभी देखें
मूल उत्तर
कोणीय v4
1) div+ ngIf+let
<div *ngIf="{ a: 1, b: 2 }; let variable">
<span>{{variable.a}}</span>
<span>{{variable.b}}</span>
</div>
2) div+ ngIf+as
राय
<div *ngIf="{ a: 1, b: 2, c: 3 + x } as variable">
<span>{{variable.a}}</span>
<span>{{variable.b}}</span>
<span>{{variable.c}}</span>
</div>
component.ts
export class AppComponent {
x = 5;
}
3) यदि आप रैपर नहीं बनाना चाहते हैं जैसे divआप उपयोग कर सकते हैंng-container
राय
<ng-container *ngIf="{ a: 1, b: 2, c: 3 + x } as variable">
<span>{{variable.a}}</span>
<span>{{variable.b}}</span>
<span>{{variable.c}}</span>
</ng-container>
जैसा कि @ कीथ ने टिप्पणियों में उल्लेख किया है
यह ज्यादातर मामलों में काम करेगा लेकिन यह एक सामान्य समाधान नहीं है क्योंकि यह परिवर्तनशील सत्य पर निर्भर करता है
अन्य दृष्टिकोण के लिए अद्यतन देखें।