import { Component, OnInit,ViewChild,ElementRef } from '@angular/core';
import {Http,Headers} from "@angular/http";
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, Subscription } from 'rxjs';
import { map, filter, debounceTime, tap, switchAll, count } from 'rxjs/operators';
@Component({
selector: 'app-content',
templateUrl: './content.component.html',
styleUrls: ['./content.component.css']
})
export class ContentComponent implements OnInit {
constructor(private http:Http) {
}
@ViewChild('abcde') abcde: ElementRef;
private typeTerm = new Subject<string>();
public result:string = "";
}
fTyping(a){
this.result = "abcd"; //It Working
if (this.timeout){ clearTimeout(this.timeout);}
this.timeout = setTimeout(function() {
this.result = "efgh"; //**Not Working**
const url = "http://localhost:81/api/films/GetFilms",body=JSON.stringify({username:"admin",password:"admin",page:1,"search":a,sort:this.txtSort});
const requestHeader = new Headers({ 'Content-Type': 'application/json' });
http1.post(url,body,{ headers : requestHeader }).toPromise().then(res=>{
temp=res.json();
console.info(res.json().length);
this.isLoading=false;
if (res.json().length<10){
this.isData=false;
}
else
this.isData=true;
}).catch(x=>{
this.result=JSON.stringify({Name:"No data",Title:"No Data",Manufacture:"No Data",Country:"No Data"});
this.isLoading=false;
this.isData=false;
});
},500);
}
Problem: Why in function setTimeoutthis.result not woking. Is there any way for it to work in function setTimeout?
Спасибо, ты спас меня! Большое спасибо. Я так благодарен ♥♥





на самом деле setTimeout создает свою собственную эту область в боковой функции, поэтому она не работает, вы можете сохранить ссылку на это в другой переменной
var that=this;
this.timeout = setTimeout(function() {
that.result = "efgh"; //use that
},500);
или вы можете использовать функцию стрелки (функция стрелки не создает свою собственную эту область)
setTimeout(() => {
this.result = "efgh";
},500)
Попробуйте использовать функцию стрелки для обратного вызова setTimeout
setTimeout(() => {