Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2-从http加载前显示数据_Angular - Fatal编程技术网

Angular2-从http加载前显示数据

Angular2-从http加载前显示数据,angular,Angular,我在从服务器异步加载数据时遇到问题。视图中没有任何数据,因为渲染是在加载之前进行的。 这是我的密码: app.component.ts: user: User = new User() ngOnInit() { this.userService.getUser(1).then(usr => { this.user = usr; }).catch((error: any) => { }); } app.component.ht

我在从服务器异步加载数据时遇到问题。视图中没有任何数据,因为渲染是在加载之前进行的。 这是我的密码: app.component.ts:

    user: User = new User()
    ngOnInit() {
    this.userService.getUser(1).then(usr => {
      this.user = usr;
    }).catch((error: any) => {

    });
  }
app.component.html:

<p>{{user.name}}</p>
{{user.name}


我认为数据会动态变化。问题在哪里?谢谢

尝试使用elvis操作员这可能会有所帮助

<p>{{user?.name}}</p>
{{user?.name}

甚至可能尝试使用ngIf

<div *ngIf = "user">
    user.name
</div>

用户名
{{user.name}


{{user.name}

你能试试
{{user?.name}

吗?是的,结果是一样的。我没有看到用户名尝试在异步调用中记录
this.user
分配后,向我们显示内容您是否更改了\parent组件中的
DetectionStrategy
。如果您在下面的回答中按OP检查注释。数据结构已更改,因此显然
用户
不再具有属性
名称
;)这不是一个好的解决方案。我看不到用户名。我为store user和getter及setter方法创建了服务我将断点设置为get set,首先调用get method和than set method用户是否登录到组件检查?很抱歉,问题在于我从服务器获取的数据中。数据结构已经改变,我不知道。它起作用了now@bluray那你为什么接受这个答案呢?这并不能解决问题。
<p>{{user.name}}</p>
<div *ngIf="user">
    {{user.name}}
</div>