Courses are not getting loaded dynamically in dropdown with class selectpicker.
I have added dropdown with selectpicker class in angular. It gets initiated but remains empty. The courses are to be added after being fetched from MongoDB. I have written course fetching logic in ngOnInit() function of ts file. But the array of course is not getting displayed in dropdown selectpicker. The courses are getting logged in console after fetching. If I directly assign array to CourseAddChoices in ngOnInit(), it loads value in dropdown, but I am fetching data from MongoAtlas.
HTML file code
<label class="col-sm-3 col-form-label">Courses</label>
<div class="col-sm-4">
<div class="input-group-prepend ">
<div class="addPrepend input-group-text bg-success">
Add
</div>
<select class="selectpicker" multiple data-live-search="true" id="add_courses" name="add_courses" (ngModel)="add_courses" #add_course="ngModel">
<option *ngFor="let course of CourseAddChoices" [value]="course">{{course}}
</option>
</select>
</div>
</div>
ts file (It is large file, but the ngOnInit function is shown below)
CoursesList: any[] = []; // all courses which are offered
UserData: any[] = []; // user data
UserRegisteredCourses: any[] = []; // Courses which the user have completed
CourseAddChoices: any[] = []; // Courses which user can add as registered
TempArray: any[] = []; // This array will be used in checking difference in CoursesList and UserRegisteredCourses
ngOnInit() {
this.authenticationService.authenticate();
// fetch courses using getcourse service
this._courseService.fetchCourses().subscribe(
data => {
this.CoursesList = data['Courses'];
this._userdata.fetchUserData().subscribe(
data => {
this.UserData = data['User'];
this.UserRegisteredCourses = data['User']['courses'];
for (var i = 0; i < this.CoursesList.length; i++) {
this.TempArray[this.CoursesList[i]['Name']] = true;
}
for (var i = 0; i < this.UserRegisteredCourses.length; i++) {
if (this.TempArray[this.UserRegisteredCourses[i]]) {
delete this.TempArray[this.UserRegisteredCourses[i]];
}
}
for (var course in this.TempArray) {
this.CourseAddChoices.push(course);
}
},
error => {
console.log("UserProfileFetchError: ", error);
}
);
},
error => {
console.log("CourseFetchError: ", error);
},
);
}
this.CourseAddChoices.push(course);, is it actually pushing items in?CourseAddChoicesan array of strings? It is hard to tell since there are no types defined.