[英]Angular 2 Populate select drop down values from Mongoose DB
我需要從Mongo DB貓鼬中獲取選擇下拉列表值。
在我的角度2中,我有一些靜態的東西:
更新的代碼:
我想實現這樣的目標,我嘗試過:
<form role="form">
<fieldset class="form-group">
<label>Select Category</label><br/><br/>
<select [(ngModel)]="selectedObject" name="first" class="form-control">
//Static data
<!--<option>Select</option>
<option>Juices</option>
<option>Chats</option>
<option>Meals</option>-->
<option *ngFor="let c of categories"
[value]="c"
[selected]="c.categoryName == selectedObject.categoryName"
>
{{c.categoryName}}
</option>
</select>
</fieldset>
</form>
在我的component.ts中,我有類似以下內容:
export class BlankPageComponent implements OnInit {
selectedObject = null;
categories = [];
constructor(private addproductService: AddproductService,
private flashMessage: FlashMessagesService,
private router: Router) { }
ngOnInit() {
const categories = {
category_name: this.category_name
}
this.addproductService.loadData(categories).subscribe(data => {
});
\\ src \\ app \\ shared \\ services \\ addproduct.service.ts
export class AddproductService {
categories: any;
loadData(pList: any) {
this.categories = pList;
if (this.categories.length > 0) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://10.22.*.*:3000/categories/get', this.categories, { headers: headers })
.map(res => res.json());
//this.selectedObject = this.categories[0];
}
}
截至目前的錯誤 :TypeError:無法讀取未定義的屬性“ subscribe”
但是我需要從后端獲取下拉列表的值(將它們綁定)
在我的貓鼬后端中,我有一個字段名稱為:category_name的文檔,其值如:Juice,Meals,Chats等;在郵遞員中,我使用API來獲取它們:
http://10.22 。 。 :3000 /類別/獲取
我能夠使用GET請求從nodejs提取類別,但是如何綁定選擇控件並從貓鼬填充數據
.ts
categories = ['Juices', 'Chats', 'Meals'];
selectedCategory: string;
.html
<form role="form">
<fieldset class="form-group">
<label>Select Category</label><br/><br/>
<select [(ngModel)]="selectedCategory" class="form-control">
<option disabled>--Select--</option>
<option *ngFor="let category of categories" [value]="category">{{category}}</option>
</select>
</fieldset>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.