0

I need to fetch the select drop down list vales from Mongo DB mongoose.

In my angular 2 i have something which is static as:

UPDATED CODE:

I want to achieve something like this and I have tried like :

<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>

In my component.ts i have something like:

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

But i need to get the values of the drop down from backend(bind them)

In my mongoose backend i have a document with field name : category_name which has values like : Juice, Meals, Chats etc and in postman i fetch them like using API:

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

I am able to fetch the category from nodejs using GET request, But how to bind the select control and populate data from mongoose

1 Answer 1

1

.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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Martin, i found that , but how do i get the values from backend, via API, as specified in my question
Have updated my question @Martin, which i need to achieve something as shown , but im unsure of subscribe and flatMap operator, need help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.