绑定textarea中的值

时间:2016-09-18 18:27:23

标签: angular typescript angular2-ngmodel

我正在尝试在Angular2中进行最简单的双向绑定。我想在我的组件和它的模板之间共享一个变量。

我的模板是:

<textarea [(ngModel)]="currentQuery"></textarea>

我的组件是:

import { Component } from '@angular/core';
import { ViewChild } from '@angular/core';
import { OnInit } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'vs-home',
  templateUrl: 'home.component.html'
})

export class HomeComponent {
    private currentQuery: string = '';
}

根据文档,这应该有用,但我得到了:

Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'textarea'. ("
    <div class="query-bar-container">
        <textarea [ERROR ->][(ngModel)]="currentQuery"></textarea>
        <!-- <button type="button" class="btn btn-default" (click"): HomeComponent@2:15

1 个答案:

答案 0 :(得分:12)

@NgModule({
  ...
  imports: [
      /* BrowserModule or CommonModule */, 
      FormsModule /* or RectiveFormsModule */]
  ...
})