如何在Angular表达式中使用别名?

达斯节

我正在使用Angular 7

我有以下模板

<div>
{{myService.userInfo.firstName}} {{myService.userInfo.lastName}}
</div>

我想通过代替user代替myService.userInfo

<div>
{{user.firstName}} {{user.lastName}}
</div>

我该如何像AngularJS中的ng-init一样执行此操作。

萨达姆·波吉(Saddam Pojee)

是的,您可以在NgIf的帮助下进行操作NgForOf

解:

因此,在您的情况下,它将是:

<div *ngIf="myService.userInfo as user">
  {{user.firstName}} {{user.lastName}}
</div>

Angular文档供参考:
https : //angular.io/api/common/NgIf#storing-conditional-result-in-a-variable https://angular.io/api/common/NgForOf#local-variables

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章