обновлять привязку html после каждого изменения в машинописи

Итак, из комментариев я сделал это изменение в своем app.js:

app.post('/', function(req, res) {

    res.send(req.body.data);

});

Тогда в моем index.js:

var xhr = new XMLHttpRequest();
    xhr.open("POST", "/", true);
    xhr.setRequestHeader('Content-Type', 'application/json');
    xhr.send(JSON.stringify({
        data: data
    }));

    xhr.onreadystatechange = function(){
      if (xhr.readyState === XMLHttpRequest.DONE) {
        if (xhr.status === 200) {
          console.log(xhr.responseText);
        } else {
          alert('There was a problem with the request.');
        }
      }
    }

В принципе, я отсутствовал res.send() изначально. И когда эта информация отправляется, я смог console.log ответить на мой index.js.

0
задан waleed Jubeh 18 January 2019 в 18:01
поделиться

1 ответ

Вот один из подходов, который вы можете попробовать:

import { Component } from '@angular/core';
import { interval } from 'rxjs';
import { take } from 'rxjs/operators';


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  name = '';
  ngOnInit() {
    interval(500)
    .pipe(
      take(5)
    )
    .subscribe(_ =>
      this.name += 'a'
    );
  }
 }

демо https://stackblitz.com/edit/angular-6pkbhk?file=src%2Fapp%2Fapp.component .ts

В качестве альтернативы вы все еще можете использовать простой старый метод обещаний, который более близко соответствует вашему первоначальному подходу,

  import { Component } from '@angular/core';
  import { interval } from 'rxjs';
  import { take } from 'rxjs/operators';


  @Component({
    selector: 'my-app',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
  })
  export class AppComponent {
    title = '';
    ngOnInit() {

      this.sleep(200)
        .then(() => this.title = 'a')
        .then(() => this.sleep(200))
        .then(() => this.title += 'a')
        .then(() => this.sleep(200))
        .then(() => this.title += 'a')
        .then(() => this.sleep(200))
        .then(() => this.title += 'a')
        .then(() => this.sleep(200))
        .then(() => this.title += 'a')

   // same as 
  /*  let promise: any = this.sleep(delay)
      .then(() => this.title = 'a');

    for (let i = 0; i < 4; i++) {
      promise = promise
        .then(() => this.sleep(delay))
        .then(() => this.title += 'a')
    } */
    }

    sleep(milliseconds) {
      let resolve;
      let promise = new Promise((_resolve) => {
        resolve = _resolve;
      });
      setTimeout(() => resolve(), milliseconds);
      return promise;
    }
  }

demo https: // stackblitz. ком / редактировать / угловой d8ur5y? файл = ЦСИ% 2Fapp% 2Fapp.component.ts

0
ответ дан ABOS 18 January 2019 в 18:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: