数据未加载到 Angular 视图中,但出现在控制台日志中

Data is not getting loaded in Angular view but present in the console log

此处组件正在从服务订阅数据,我清楚地看到订阅范围内的数据,但由于某种原因,初始化上的 ng 在订阅之前被加载,这就是为什么我没有在view.Can谁帮我解决这个问题。

学生服务

@Injectable({
providedIn: 'root'
})
export class StudentService {
  constructor(private http:HttpClient){}

  students=[];
  // I HAVE MY API
  //REMOVED THIS FOR SECURITY PURPOSE
  baseUrl="";

  getStudents(){
     return this.http.get<GetResponseStudent>(this.baseUrl+"students").pipe(
       map(
         response => {
           this.students=response.students

           return this.students;
          }
     )
     );
  }

}

interface GetResponseStudent{
    status:true,
    students:Student[]
}


//StudentComponent

@Component({
  selector: 'app-student',
  templateUrl: './student.component.html',
  styleUrls: ['./student.component.scss']
})
export class StudentComponent implements OnInit {

  students:Student[]=[];
  constructor(
    private studentService:StudentService,
    private router:Router,

  ) {
   }
  ngOnInit(): void {
    // this.studentService.students.subscribe(
    //   data => {
    //    this.students=data;
    // });

    this.studentService.getStudents().subscribe(
      data => {
        this.students=data;
        console.log(this.students);
      }
    )

   console.log(this.students);
  }
}

使用ChangeDetectorRef

例如:

import { ChangeDetectorRef } from 
'@angular/core';
 constructure(private cd: ChangeDetectorRef) {}

someMethod() {
this.cd.detectChanges();
}