简体   繁体   English

ngModel未绑定角度4中的select

[英]ngModel is not binding select in angular 4

I have a form in my application which contains title , price , category (select) and imagUrl data. 我的应用程序中有一个表单,其中包含titlepricecategory (select)imagUrl数据。 I applied ngModel with every field and it's working fine except the select element. 我对每个字段都应用了ngModel ,除了select元素之外,它都工作正常。 When I console.log() form data I get the value of every field but the value of the select element which is undefined. 当我console.log()表单数据时,我得到每个字段的值,但未定义的select元素的值。 I imported the AngularForms module in app.module.ts 我进口AngularForms模块中app.module.ts

this is my product-form-component-html code 这是我的product-form-component-html代码

<form #f="ngForm" (ngSubmit)="save(f.value)" >
  <div class="form-group">
    <label for="title">Title</label>
    <input ngModel name="title" id="title" type="text" class="form-control">
  </div>
  <div class="form-group">
    <label for="price">Price</label>
    <div class="input-group">
      <span class="input-group-addon">$</span>
      <input ngModel name="price" id="price" type="number" class="form-control">
    </div>
  </div>
  <div class="form-group">
    <label for="category">category</label>
    <select ngModel name="category" id="category"  class="form-control">
      <option value=""></option>
      <option *ngFor="let c of categories$ | async" [value]="c.key$">
        {{ c.name }}
      </option>
    </select>
  </div>
  <div class="form-group">
      <label for="imageUrl">image URL</label>
      <input ngModel name="iamgeUrl" id="imageUrl" type="text" class="form-control">
  </div>
  <button class="btn btn-primary">Save</button>
</form>

this is product-form-component.ts file 这是product-form-component.ts文件

import { Component, OnInit } from '@angular/core';
import { CategoryService } from '../../category.service';
import { ProductService } from '../../product.service';

@Component({
  selector: 'app-product-form',
  templateUrl: './product-form.component.html',
  styleUrls: ['./product-form.component.css']
})
export class ProductFormComponent implements OnInit {
  categories$;

  constructor(categoryService: CategoryService, private productService: ProductService ) { 
    this.categories$ = categoryService.getCategories();
  }

  ngOnInit() {
  }

  save(product) {
    console.log(product);
    this.productService.create(product);
  }
}

this is product-service.ts 这是product-service.ts

import { Injectable } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';

@Injectable()
export class ProductService {

  constructor(private db: AngularFireDatabase) { }

  create(product) {
    return this.db.list('/products').push(product);
  }
}

what am I doing wrong? 我究竟做错了什么? please help me with detailed answer 请帮助我详细的答案

my Firebase Database 我的Firebase数据库 在此处输入图片说明

this is the answer [value]="c.$key" . 这就是答案[value]="c.$key" I was assigning [value]="c.key$" that is why i was getting error. 我分配了[value]="c.key$" ,这就是我遇到错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM