简体   繁体   English

如何使jQuery在Angular 2中以正确的顺序运行

[英]How to get jQuery to run in right sequence in Angular 2

Please note that I'm a beginner with Angular 2 and asynchronous loading. 请注意,我是Angular 2和异步加载的初学者。 The following questions helped me on the right track, but don't seem to answer this exact question: 以下问题有助于我走上正轨,但似乎无法回答这个确切的问题:

I have a jQuery script that should run in an Angular 2 project, but cannot seem to find the right way to get these scripts to load in the right sequence. 我有一个应在Angular 2项目中运行的jQuery脚本,但似乎找不到正确的方法来使这些脚本按正确的顺序加载。 The script finds a certain DOM element and turns it into a custom radial slider (using http://roundsliderui.com ). 该脚本找到一个特定的DOM元素,并将其变成一个自定义的径向滑块(使用http://roundsliderui.com )。

This is the sequence I want: 这是我想要的顺序:

  1. JSON call to get data JSON调用以获取数据
  2. ***.component.html gets filled with <li *ngFor="let item of items" ...</li> ***。component.html中充满了<li *ngFor="let item of items" ...</li>
  3. roundSlider script loads and transform li elements to roundSliders (radial slider controls) roundSlider脚本加载li元素并将其转换为roundSliders(径向滑块控件)

What seems to be happening is that the Angular2 Component that handles the JSON call always loads (or finishes) after the jQuery script that displays the controls for the loaded data. 似乎正在发生的事情是,处理JSON调用的Angular2组件总是显示用于加载数据的控件的jQuery脚本之后加载(或完成)。 This means that the jQuery script successfully loads, but has no DOM elements to reference. 这意味着jQuery脚本已成功加载,但没有DOM元素可供参考。

For some reason it just doesn't help to use Lifecycle hooks like ngAfterViewInit , which sounds like it should be sufficient. 由于某些原因,使用诸如ngAfterViewInit类的Lifecycle挂钩ngAfterViewInit ,这听起来就足够了。 The only lifecycle hook that does work is ngAfterViewChecked, but this causes trouble with using the actual control that is generated (because it then also fires the script when you use the slider). 唯一的生命周期挂钩, 的工作是ngAfterViewChecked,但这会导致麻烦,使用所产生的实际控制(因为它,当您使用滑块,然后又触发脚本)。

I thought it would be smart to try and use the ngOnInit hook to call the scripts in sequence, but that also doesn't work. 我认为尝试使用ngOnInit挂钩按顺序调用脚本会很聪明,但这也不起作用。

How can a simple jQuery script be tought to wait for a JSON call to finish getting data and the DOM elements ( ngFor ) to load? 一个简单的jQuery脚本如何才能难以等待JSON调用完成数据获取和DOM元素( ngFor )的加载?

Without you showing some of your code, I guess it's the usual question about how to use Observables . 如果没有显示一些代码,我想这是关于如何使用Observables的常见问题。

Indeed ngAfterViewInit seems like the right place to start, but there's more to it, since you're talking about making a JSON call. 确实, ngAfterViewInit似乎是一个合适的起点,但是还有更多的东西,因为您正在谈论进行JSON调用。 When the JSON gets returned from http.get/post , you're in a callback. 当JSON从http.get/post返回时,您正在回调中。 That callback fires whenever the async operation finishes, hence everything after that http call might get executed before the call finishes. 每当异步操作完成时都会触发该回调,因此该http调用之后的所有内容都可能在调用完成之前执行。

I'd suggest putting your JQuery calls into that subscribe callback along the lines of this example: 我建议按照以下示例将您的JQuery调用放入该订阅回调中:

this.http.get("url")
    .map(res => res.json())
    .subscribe(data => {
        this.data = data;
        $("selector")....
    });

EDIT: 编辑:

On second thought, the view might not have rendered your data yet at this point. 再次考虑,该视图可能尚未呈现您的数据。 So maybe you still could use ngAfterViewChecked and check, if your data variable is filled and maybe have a boolean variable holding the initialized state so it only executes once after your data has been loaded. 因此,也许您仍然可以使用ngAfterViewChecked并检查data变量是否已填充,并且布尔变量是否保持initialized状态,以便仅在加载数据后执行一次。

ngAfterViewInit() {
    if (this.data != null && !this.initialized) {
        this.initialized = true;
        $("selector")...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM