【发布时间】:2020-05-06 23:10:05
【问题描述】:
我在angular 7 工作,我必须执行简单的任务,有一个组件teacher-details 以表格格式显示注册教师列表(使用节点 js 从数据库中获取)并在新表格时更新表格添加了教师记录,我还有一个teacher.service.ts,其中包含教师数组并包含方法getTeacher() 和addTeacher(),因此教师组件使用此服务来显示记录。
我的问题是当我输入教师的新记录时,它没有更新到 UI/页面,但是当我重新加载它时它会更新,但是我想在输入记录后立即动态更新页面。
teacher-details.component.ts
@Component({
selector: 'app-teacher-details',
templateUrl: './teacher-details.component.html',
styleUrls: ['./teacher-details.component.css']
})
export class TeacherDetailsComponent implements OnInit {
private teachers: Teacher[] = []
constructor(private teacherServ: TeachersService, private auth: AuthService) {
}
ngOnInit() {
this.auth.getTeacherRecords().subscribe(
res => this.teachers = res.message,
err => console.log(err)
),
this.teachers = this.teacherServ.getTeachers();
}
}
teacher.service.ts
@Injectable()
export class TeachersService {
teacherChanged = new EventEmitter<Teacher[]>();
private teachers: Teacher[] = [];
constructor(private auth: AuthService) {}
getTeachers() {
return this.teachers;
}
addTeachers(teacher: any) {
this.auth.addTeacher(teacher)
.subscribe(
res => console.log(res),
err => console.log(err)
);
this.teachers.push(teacher);
}
}
我还分享了我的身份验证服务,该服务从数据库中获取详细信息。
@Injectable()
export class AuthService {
private addTeacherUrl = "http://localhost:3000/api/addTeacher"
private getTeacherRecordsUrl = "http://localhost:3000/api/getTeacherRecords"
addTeacher(teacherRecord: any) {
console.log("Inside addTEacher servvice")
console.log(teacherRecord)
return this.http.post<any>(this.addTeacherUrl, teacherRecord)
}
getTeacherRecords() {
return this.http.get<any>(this.getTeacherRecordsUrl, {
observe: "body"
})
}
}
teacher-details.component.html
<div class="card">
<div class="card-header border-0">
<div class="row align-items-center">
<div class="col">
<h3 class="mb-0">Teacher Details</h3>
</div>
</div>
</div>
<div class="table-responsive">
<!-- Projects table -->
<table class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th scope="col">Teacher Name</th>
<th scope="col">Teacher subject</th>
<th scope="col">Teacher branch</th>
<th scope="col">Teacher Semester</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let teachers of teachers">
<th scope="row">
{{teachers.fullName}}
</th>
<td>
{{teachers.subject}}
</td>
<td>
{{teachers.branch}}
</td>
<td>
{{teachers.semester}}
</td>
</tr>
</tbody>
</table>
</div>
</div>
我很困惑我该怎么办,请在这里帮帮我。
更新:-
我根据建议做了如下修改:
auth.service.ts
@Injectable()
export class AuthService {
private addTeacherUrl = "http://localhost:3000/api/addTeacher"
private getTeacherRecordsUrl = "http://localhost:3000/api/getTeacherRecords"
subject = new Subject<Teacher[]>();
teachers: Teacher[]
constructor(private http: HttpClient) { }
addTeacher(teacherRecord: any) {
console.log("Inside addTEacher servvice")
console.log(teacherRecord)
this.teachers.push(teacherRecord)
this.subject.next(this.teachers)
return this.http.post<any>(this.addTeacherUrl, teacherRecord)
}
getTeacherRecords() {
return this.http.get<any>(this.getTeacherRecordsUrl, {
observe: "body"
})
}
}
teacher-details.component.ts
@Component({
selector: 'app-teacher-details',
templateUrl: './teacher-details.component.html',
styleUrls: ['./teacher-details.component.css']
})
export class TeacherDetailsComponent implements OnInit {
teachers: Teacher[];
constructor(private teacherServ: TeachersService, private auth: AuthService) {
}
ngOnInit() {
this.auth.getTeacherRecords().subscribe(
res => this.teachers = res.message,
err => console.log(err)
);
this.auth.subject.subscribe(
res => console.log(res),
err => console.log(err)
)
}
}
重新加载页面后仍会更新数据
谢谢。
【问题讨论】:
标签: angular