Angular2 - 为什么 JsonPipe 用引号将模型属性括起来?
Angular2 - Why does JsonPipe surround model properties with quotes?
在 Angular 2 中,我有一个组件订阅 returns 模型的服务,然后使用 JsonPipe 在
我的代码(假设有一个服务可以正确填充模型):
/* example.ts */
export interface Example {
id: number;
description: string;
}
/* example.component.ts */
export class ExampleComponent {
public example: Object = Object;
constructor(private _exampleService: ExampleService) {
this._getExample();
}
private _getExample() {
this._exampleService
.getExample()
.subscribe(example => this.example = <Example> example);
}
}
/* example.template.ts */
<form>
Description: <textarea>{{example.description | json}}</textarea>
</form>
这将呈现如下所示的
_______________________________
Description: | "this is the description" |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
没有理由用引号将字符串括起来。这是怎么回事?
来自 javascript 文档 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify('foo'); // '"foo"'
现在让我们看看angular2源码:
@Pipe({name: 'json', pure: false})
@Injectable()
export class JsonPipe implements PipeTransform {
transform(value: any): string { return Json.stringify(value); }
}
https://github.com/angular/angular/blob/master/modules/%40angular/common/src/pipes/json_pipe.ts#L15
https://github.com/angular/angular/blob/master/modules/%40angular/facade/src/lang.ts#L422
在 Angular 2 中,我有一个组件订阅 returns 模型的服务,然后使用 JsonPipe 在
我的代码(假设有一个服务可以正确填充模型):
/* example.ts */
export interface Example {
id: number;
description: string;
}
/* example.component.ts */
export class ExampleComponent {
public example: Object = Object;
constructor(private _exampleService: ExampleService) {
this._getExample();
}
private _getExample() {
this._exampleService
.getExample()
.subscribe(example => this.example = <Example> example);
}
}
/* example.template.ts */
<form>
Description: <textarea>{{example.description | json}}</textarea>
</form>
这将呈现如下所示的
_______________________________
Description: | "this is the description" |
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
没有理由用引号将字符串括起来。这是怎么回事?
来自 javascript 文档 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
JSON.stringify('foo'); // '"foo"'
现在让我们看看angular2源码:
@Pipe({name: 'json', pure: false})
@Injectable()
export class JsonPipe implements PipeTransform {
transform(value: any): string { return Json.stringify(value); }
}
https://github.com/angular/angular/blob/master/modules/%40angular/common/src/pipes/json_pipe.ts#L15
https://github.com/angular/angular/blob/master/modules/%40angular/facade/src/lang.ts#L422