简体   繁体   English

Angular 2 HTTP响应动画

[英]Angular 2 HTTP response animations

I'd like to run jQuery animations after a HTTP response has been received by the client. 我想在客户端收到HTTP响应后运行jQuery动画。

My web service, it's a simple web service that uses AngularJS's Http functionality: 我的Web服务是使用AngularJS的Http功能的简单Web服务:

import {Component, View, Injectable} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {JsonObj} from './JsonObj.class';
@Injectable()
export class WebService {
    private http: Http;

constructor(http: Http) {
    this.http = http;
}

get(url: string) {
    return this.http.get(url)
        // Call map on the response observable to get the parsed people object
        .map(res => {
            let _d = res.json();
            return new JsonObj(_d.code, _d.status, _d.data, _d.error);
        });
    }
}

The JsonObj, this is simply an object that hosts the data from the web service: JsonObj,这只是一个承载Web服务中数据的对象:

export class JsonObj {
private code: Number;
private status: String;
private data: any;
private error: Array<any>;

constructor(_c: Number, _s: String, _d: Array<any>, _e: Array<any>) {
    this.code = _c;
    this.status = _s;
    this.data = _d;
    this.error = _e;
}

getCode() {
    return this.code;
}
getStatus() {
    return this.status;
}
getData() {
    return this.data;
}
getError() {
    return this.error;
}
}

The Component making the web service call: 进行Web服务调用的组件:

import {Component, ElementRef} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {Rotate} from './Rotate.class';
import {WebService} from '../../App/js/WebService.class';

@Component({
selector: 'Reviews',
viewProviders: [HTTP_PROVIDERS],
templateUrl: 'assets/templates/reviews.template.html'
})
export class Reviews {
public review: Array<any>;
private speed = 5000;
private outElem = null;
private parentElem = null;
private interval = null;

constructor(ws: WebService, el: ElementRef) {
    console.log('Reviews');

    ws.get('assets/temp_quotes.json').subscribe(
        data => this.review = data.getData().reviews,
        err => console.log('Error getting Review feed', err.getCode(), err.getError()),
        () => {
            console.log('Successful retrieval of the Review feed', this.review);
            let rotator = new Rotate(el);
            rotator.loop();
        }
    );
}
}

The template, the ngFor loops through the data from the web service and outputs it: 模板ngFor循环遍历Web服务中的数据并将其输出:

<div class="feedback-wrapper">
    <div class="quote_overlay hide" style="display: none;"></div>
    <h4>What people have to say about us...</h4>
    <ul>
        <li *ngFor="#el of review" class="hide" style="display: none;">
            <p class="quote">{{el.quote}}</p>
            <p class="author">{{el.name}}, United Kingdom<br>Reviews.co.uk, {{el.date}}</p>
        </li>
    </ul>
    <p class="show-right"><a href="/testimonials.htm">Read all Reviews</a></p>
</div>

My problem is that when the Component calls this: 我的问题是,当组件调用此命令时:

let rotator = new Rotate(el);
rotator.loop();

the el (ElementRef) content from the web service has not been populated even though the dom has been updated with the response. 即使已使用响应更新了dom,也未填充Web服务中的el(ElementRef)内容。 It seems that rotator.loop() has been called too soon. 似乎rotator.loop()的调用时间过早。 I want to do an animation that loops through each of the list items one at a time. 我想做一个动画,一次遍历每个列表项。 Any idea on how that can be done correctly? 关于如何正确完成任何想法?

Whatever comes last starts the animation 最后发生的一切都会开始播放动画

constructor(ws: WebService, el: ElementRef) {
  console.log('Reviews');
  private _initialized: boolean false;

  ws.get('assets/temp_quotes.json').subscribe(
    data => this.review = data.getData().reviews,
    err => console.log('Error getting Review feed', err.getCode(), err.getError()),
    () => {
        console.log('Successful retrieval of the Review feed', this.review);
        if(!this._initialized) {
          this._initialized = true; 
        } else {
          this.startAnimation();
        }
    });
  }

  ngAfterViewInit() {
    if(!this._initialized) {
      this._initialized = true;
    } else {
      this.startAnimation();
    }
  }
  startAnimation() {
    let rotator = new Rotate(el);
    rotator.loop();
  }
}

You could also create an observable and add an event when getData() completed and another one in ngAfterViewInit() . 您还可以创建一个可观察对象,并在getData()完成时添加一个事件,并在ngAfterViewInit()添加另一个事件。 Subscribe on the observable, ignore the first event and execute startAnimation() on the 2nd and than cancel the subscription. 订阅可观察对象,忽略第一个事件并在第二个事件上执行startAnimation() ,然后取消订阅。 I don't use TS myself therefore it's hard for me to provide a code example. 我自己不使用TS,因此很难提供代码示例。

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

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