Actualizar
Podemos simplemente crear directivas como *ngIfy llamarlo*ngVar
ng-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);
}
}
con esta *ngVardirectiva podemos usar lo siguiente
<div *ngVar="false as variable">
<span>{{variable | json}}</span>
</div>
o
<div *ngVar="false; let variable">
<span>{{variable | json}}</span>
</div>
o
<div *ngVar="45 as variable">
<span>{{variable | json}}</span>
</div>
o
<div *ngVar="{ x: 4 } as variable">
<span>{{variable | json}}</span>
</div>
Ejemplo de Plunker Angular4 ngVar
Ver también
Respuesta original
V4 angular
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
ver
<div *ngIf="{ a: 1, b: 2, c: 3 + x } as variable">
<span>{{variable.a}}</span>
<span>{{variable.b}}</span>
<span>{{variable.c}}</span>
</div>
componente.ts
export class AppComponent {
x = 5;
}
3) Si no quieres crear un contenedor como divpuedes usarng-container
ver
<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>
Como @Keith mencionó en los comentarios
esto funcionará en la mayoría de los casos, pero no es una solución general ya que depende de que la variable sea verdadera
Ver actualización para otro enfoque.