【发布时间】:2020-11-29 04:40:05
【问题描述】:
我正在用一个名为“getQuestionsWithInterviewId”的函数填充我的字符串“Questions”,但是当我在 ngOnInit 和 ngAfterContentInit 方法中使用 console.log 调用它时,它看起来是空的。
import { Component, OnInit } from '@angular/core';
import { QuestionService } from '../_services/question.service';
import { Question } from '../_models/model';
@Component({
selector: 'app-interview',
templateUrl: './interview.component.html'
})
export class InterviewComponent implements OnInit {
questions: Question[]=[];
constructor(private questionService: QuestionService) {
}
ngOnInit(): void {
}
ngAfterContentInit() {
this.getQuestionsWithInterviewId(1);
console.log(this.questions);
$(".tab-wizard").steps({
headerTag: "h6",
bodyTag: "section",
transitionEffect: "fade",
titleTemplate: '<span class="step">#index#</span> #title#',
labels: {
finish: "end"
},
onFinished: function (event, currentIndex) {
alert("end");
}
});
}
getQuestionsWithInterviewId(interviewId: number) {
this.questionService.getQuestionsWithInterviewId(interviewId).subscribe(a => {
this.questions = a;
},
error => {
console.log(error);
});
}
}
但是当我在 component.html 页面上使用 'questions' 数组时,我可以看到结果。
如果我在 'getQuestionsWithInterviewId' 函数中执行 console.log 操作,我可以获得结果。
getQuestionsWithInterviewId(interviewId: number) {
this.questionService.getQuestionsWithInterviewId(interviewId).subscribe(a => {
this.questions = a;
console.log(this.questions);
},
error => {
console.log(error);
});
}
question.service.ts 页面;
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Question } from '../_models/model';
@Injectable({
providedIn: 'root'
})
export class QuestionService {
baseUrl: string = 'https://localhost:44388/api/questions/';
constructor(private http: HttpClient) {
}
getQuestionsWithInterviewId(interviewId: number): Observable<Question[]> {
return this.http.get<Question[]>(this.baseUrl +
"GetQuestionsWithInterviewId/" + interviewId);
}
}
【问题讨论】:
标签: javascript arrays angular typescript ngoninit