Angular 2 - 无法绑定到' ngModel'因为它不是“输入”的已知属性。

时间:2016-08-12 00:03:27

标签: angularjs forms angular angular2-template angular2-forms

我在plunker中有一个Angular 2 Inline Editing组件。但突然之间我得到了上述错误。发生了什么事?

Plunker代码:http://plnkr.co/edit/3AODo6YGEhvPOKzloofz?p=preview

父组件:

<inline-edit [editEnable]="edit" [(ngModel)]="editableText" (onSave)="saveEditable($event)"></inline-edit>

子组件:

<div id="inlineEditWrapper" (click)="edit(value)" class="iedit">
    <!-- Editable value -->
    <a  [hidden]="editing">{{ value }}&nbsp;<i class="glyphicon glyphicon-pencil" data-hidden="editing"></i></a>

    <!--<span class="glyphicon glyphicon-pencil" [hidden]="editing"></span>-->

    <!-- inline edit form -->
    <form  #inlineEditForm="ngForm" class="inlineEditForm form-inline" (ngSubmit)="onSubmit(value)" [hidden]="!editing">
        <div class="form-group">

            <!-- inline edit control  -->
           <input style="margin-bottom:10px;" #inlineEditControl class="form-control" [(ngModel)]="value"/>

            <!-- inline edit save and cancel buttons -->
            <br/>
           <span >
                <button type="submit" class="btn btn-primary">Change</button>
                <button class="btn btn-default" (click)="cancel(value)">Cancel</button>
           </span>

        </div>
    </form>
</div>

1 个答案:

答案 0 :(得分:2)

您可以按照以下步骤操作:

步骤1:加载角形式包

'@angular/forms': {
      main: 'bundles/forms.umd.js',
      defaultExtension: 'js'
}

第2步:导入disableDeprecatedForms,provideForms

import {disableDeprecatedForms, provideForms} from '@angular/forms';

bootstrap(App, [
   disableDeprecatedForms()
   provideForms()
]).catch(err => console.error(err));

步骤3:从inline-edit.component.ts中的“@ angular / forms”导入ControlValueAccessor,NG_VALUE_ACCESSOR

步骤4:在表单中调整输入元素:

<input style="margin-bottom:10px;" #inlineEditControl class="form-control" name="first_name" #first_name="ngModel" [(ngModel)]="value"/>

here

为我工作