0

我的任务是将现有的 UI 集成到另一个已经存在的 Angular 应用程序中,并挂断了这个......

我知道数据 + 转换在 Angular 上下文之外运行良好。

我没有看到 jquery json2html 的 npm 包,所以我尝试通过将它保存在组件目录旁边以临时方式导入该部分。

它产生ERROR TypeError: $(...).json2html is not a function...

...
import * as $ from 'jquery';
import json2html from 'json2html';
import * from './jquery.json2html.min.js';

...

$('#myTable > tbody').json2html(data,transform);
4

2 回答 2

0

jQuery在您必须在组件中添加任何内容之前angular.json,您将使用jQuery这样做

....
declare let $: any;    // you can use "jQuery" keyword instead of "$"

@component({
  selector: '...',
  templateUrl: ['...'],
  styleUrls: ['...']
})
export class JqueryExampleComponent implements onInit {

  constructor(private eleRef: ElementRef) {}

  ngOnInit() {
    $(this.eleRef.nativeElement).find('#myTable > tbody').json2html(data,transform);
  }
}

当你使用$(this.eleRef.nativeElement)它会得到dom的树根然后得到想要的component.find('selector')element

于 2018-11-09T22:42:06.873 回答
0
    npm install node-json2html


    ...
    import json2html from 'json2html';
    import * as $ from 'node-json2html';

    ...

ngAfterViewInit(){
    $('table#myTable > tbody').json2html(data,transform);

}

或者,

... 
import json2html from 'json2html';
import * as $ from './jquery.json2html.min.js';

...

$('table#myTable > tbody').json2html(data,transform);

链接:- http://www.json2html.com/

于 2018-11-09T22:20:04.260 回答