Angular 2 从 Mongoose DB 填充 select 下拉值
Angular 2 Populate select drop down values from Mongoose DB
我需要从 Mongo 数据库猫鼬中获取 select 下拉列表值。
在我的 angular 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];
}
}
Error as of now: TypeError: Cannot read property 'subscribe' of undefined
但我需要从后端获取下拉列表的值(绑定它们)
在我的 mongoose 后端中,我有一个字段名称为 category_name 的文档,其值如下:果汁、膳食、聊天等,在邮递员中,我像使用 API 一样获取它们:
http://10.22..:3000/categories/get
我可以使用 GET 请求从 nodejs 获取类别,
但是如何绑定 select 控件并从 mongoose
填充数据
.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>
我需要从 Mongo 数据库猫鼬中获取 select 下拉列表值。
在我的 angular 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];
}
}
Error as of now: TypeError: Cannot read property 'subscribe' of undefined
但我需要从后端获取下拉列表的值(绑定它们)
在我的 mongoose 后端中,我有一个字段名称为 category_name 的文档,其值如下:果汁、膳食、聊天等,在邮递员中,我像使用 API 一样获取它们:
http://10.22..:3000/categories/get
我可以使用 GET 请求从 nodejs 获取类别, 但是如何绑定 select 控件并从 mongoose
填充数据.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>