Here is the code snippet that I am currently working with:
post.component.ts
:
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { JobsService } from '../jobs.service';
@Component({
selector: 'job-post',
templateUrl: './create.template.html',
providers: [JobsService]
})
export class JobPostComponent {
job: any = {};
errorMessage: String;
constructor(private _router:Router, private _jobsService:JobsService) {}
create() {
this._jobsService
.create(this.job)
.subscribe(createdJob => this._router.navigate(['/jobs', createdJob._id]), error => this.errorMessage = error);
}
}
create.template.html
<h1>Post New Job</h1>
<form (ngSubmit)="create()" novalidate>
<div class="form-group">
<label for="isExtensible">Is Extensible</label>
<input type="checkbox" required [(ngModel)]="job.isExtensible" name="isExtensible" id="isExtensible">
</div>
<div class="form-group">
<label class="form-control-label" for="isOpenEnded">Open Ended</label>
<input type="checkbox" required [(ngModel)]="job.isOpenEnded" name="isOpenEnded" id="isOpenEnded">
</div>
<div class="form-group">
<contract-type></contract-type>
</div>
</form>
Additionally, there is another component called contract-type.component.ts
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'contract-type',
template: `
<div *ngIf="contractTypes">
<label class="form-control-label" for="contractType">Contract Type</label>
<select id="contractType" name="contractType" required [(ngModel)]="job.contractType" class="form-control">
<option value="0">Select an item</option>
<option *ngFor="let contractType of contractTypes" value="contractType.name_en">{{ contractType.name_en }}</option>
</select>
</div>
`
})
export class ContractTypeComponent {
private contractTypes;
constructor(private _http: Http) {
this._http.get('/api/contractTypes')
.subscribe(res => {
this.contractTypes = res.json();
console.log(this.contractTypes)
});
}
}
The console output can be viewed here: https://i.sstatic.net/ydyBn.png.
However, when viewing it on the browser, the dropdown list does not appear. Removing the *ngIf
directive from the div results in an error stating
cannot read property <contractTypes> of undefined
.
My goal is to have a dropdown list populated from an API response inside the create.template.html
.