What I would like to do is update my interface, which has some user information, and then when that changes I would like the results to reflect on the HTML output.
On my HTML page, I have an input, that allows the user to add an additional comment. This is all working fine, however, the user input does not actually show up on the page, but does make the necessary changes to my interface.
In the following HTML file, the user would type something into the input. That then would get saved to my interface which is the data for each student, and then would be visible here <p>{{the students.grades}}<p>
<div class='studentCard' *ngFor='let student of students | studentSearch: StudentSearch'>
<img class='studentImg' src={{student.pic}}/>
<div class='studentInfo'>
<h1>{{student.name | uppercase}}</h1>
<div class='loca'>
<p>Email: <span>{{student.email}}</span></p>
<p>Company: <span>{{student.company}}</span></p>
<p>Skill: <span>{{student.skills}}</span></p>
<p>Average: <span>{{student.average}}</span></p>
<mat-expansion-panel [hideToggle]='true' [expanded]='true'>
<p>{{student.grades}}</p>
</mat-expansion-panel>
<p>{{student.tags}}</p>
<mat-form-field>
<input matInput (keyup.enter)='add(student.id)' placeholder="Add a tag"></mat-form-field>
</div>
</div>
<div class='expand'><span class='material-icons'>add</span></div>
</div>
This code is from my component.ts file. Right now I am just trying to pass in some dummy information to see it would actually work. I can see that it updates the interface correctly as it should.
import { Component, OnInit } from '@angular/core';
import { Student } from '../student'
import { StudentsService } from '../students.service';
@Component({
selector: 'app-student-profiles',
templateUrl: './student-profiles.component.html',
styleUrls: ['./student-profiles.component.scss']
})
export class StudentProfilesComponent implements OnInit {
//implementing an interface to control the data and make it easy to access
students: Student[] = [];
StudentSearch=''
TagSearch=''
constructor(
private service: StudentsService
) { }
ngOnInit(): void {
this.service.getStudents().subscribe(
result=>result['students'].map(
x=>{this.students.push(
{
name:x.firstName+' '+x.lastName,
grades:x.grades,
average:this.computeAvg(x.grades),
company: x.company,
email: x.email,
pic: x.pic,
id: x.id,
skills: x.skill,
tags:[]
});
}
)
)
}
//find the average of all the scores of each student. Calculates the total, then devides by the length of scores array.
computeAvg(x){
let total=0
x.map(score=>{total=total+Number(score)})
return(total/x.length)
}
add(id){
this.students[id-1]['tags'].push(String('100'))
console.log(this.students[id-1])
}
}
This is the interface that I am using for the data:
name: string;
grades: number[];
average: number;
company: string;
pic: string;
email: string;
id: number;
skills: string;
tags: string[];
}
What I am not understanding is that if I use the same code, but I change it from 'tags' to 'email', then it does appear updated in the HTML.