【问题标题】:Is there a json-formatter (directive/component) for angular?有角度的 json 格式化程序(指令/组件)吗?
【发布时间】:2017-08-03 18:41:46
【问题描述】:

我正在将应用程序从 AngularJS 升级到 Angular。在 AngularJS 中,我使用 https://github.com/mohsen1/json-formatter 来显示一个美化的 json。有角度的替代品吗?

【问题讨论】:

标签: angular


【解决方案1】:

您可以使用json-formatter-js

import JSONFormatter from 'json-formatter-js';

@Directive({
  selector: 'json-formatter'
})
export class JsonFormatterDirective implements OnChanges {
  @Input() json: any;

  constructor(private elRef: ElementRef) { }

  ngOnChanges() {
    if (this.json) {
      const formatter = new JSONFormatter(this.json);
      this.elRef.nativeElement.appendChild(formatter.render());
    }
  }
}

Plunker Example

或为 angular2 创建相同的组件

Plunker Example

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 2017-04-01
    • 2021-09-28
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多