小编Ral*_*der的帖子

使用Angular 4.3 HttpClient解析日期

我目前正在转向Angular 4.3的新HttpClient.一个优点是我可以在GET方法上指定类型信息,并将返回的JSON解析为给定类型,例如

this.http.get<Person> (url).subscribe(...)
Run Code Online (Sandbox Code Playgroud)

但不幸的是,JSON中的所有日期都在结果对象中被解析为数字(可能是因为Java Date对象在后端被序列化为数字).

使用旧的Http我在调用JSON.parse()时使用了reviver函数:

this.http.get(url)
  .map(response => JSON.parse(response.text(), this.reviver))
Run Code Online (Sandbox Code Playgroud)

在reviver函数中,我从数字中创建了日期对象:

reviver (key, value): any {
  if (value !== null && (key === 'created' || key === 'modified'))
    return new Date(value);

  return value;
}
Run Code Online (Sandbox Code Playgroud)

新的HttpClient是否有类似的机制?或者在解析JSON时进行转换的最佳做法是什么?

json date angular angular-httpclient

23
推荐指数
5
解决办法
2万
查看次数

如何选择角度材料2和量角器的选项?

我正在用量角器编写一些e2e测试。我的应用程序是Angular Material 2应用程序。在我的测试中,我想选择一个按值进行md-select的选项。当我检查打开的md-select时,我看到了md-option项。选项的值在属性ng-reflect-value中。

假设我有一个值为“ optionA”的选项。如何通过该值选择某个选项?

我尝试了这个:

element(by.name('myselect')).click();
$$('.mat-option').then((options) => {
  ...
});
Run Code Online (Sandbox Code Playgroud)

在内部,我会看到正确数量的选项,但是如何选择值为“ opetionA”的选项呢?

material protractor angular

1
推荐指数
1
解决办法
4257
查看次数

标签 统计

angular ×2

angular-httpclient ×1

date ×1

json ×1

material ×1

protractor ×1