2017-07-14 49 views
0

我需要从Mongo DB mongoose中获取select下拉列表vales。Angular 2从Mongoose 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> 

在我的组件。我有类似的东西:

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 \程序\共享\服务\ 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]; 
    } 
} 

Error as of now: TypeError: Cannot read property 'subscribe' of undefined

,但我需要从后端获得的下降值下降(将它们绑定)

在我的猫鼬后端我有一个文件,字段名称:category_name,其值有:Juice,Meals,Chats等,并且在邮递员中我像使用API​​一样获取它们:

http://10.22 ..:3000/categories/get

我能够使用GET请求, 但如何绑定选择控制和猫鼬填充数据

回答

0

.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> 
+0

由于从获取的的NodeJS类@马丁,我发现,但我怎么从后端,通过API的指定我的问题 – phyme

+0

已更新我的问题@Martin,我需要实现的东西如图所示,但我不确定的订阅和flatMap运算符,需要帮忙! – phyme