ngModel:无值访问器'' [英] ngModel: No value accessor for ''
问题描述
我创建的角2.0自定义元素(<我选>
),当我包括在组件上ngModel属性,我立刻很击中以下错误:
I'm creating a custom element in Angular 2.0 (<my-select>
), and when I include the ngModel attribute on the component, I'm immediately hit with the following error:
EXCEPTION: No value accessor for '' in [myModel in App@0:195]
下面是一个plunker: http://plnkr.co/edit/wlkPWcB92YZASCwCnmcw?p = preVIEW
Here's a plunker: http://plnkr.co/edit/wlkPWcB92YZASCwCnmcw?p=preview
如果你只是注释掉的src / app.ts
以下行,组件将适当地加以渲染:
If you simply comment out the following line from src/app.ts
, the component will render appropriately:
'[ngModel]="myModel"'
我已经做了以下内容:
I've done the following:
- 导入
{FORM_DIRECTIVES}
从'angular2 /通用
- 包括
FORM_DIRECTIVES
在指令
部分中的@Component
- 初始化
基于myModel
- Imported
{FORM_DIRECTIVES}
from'angular2/common'
- Included
FORM_DIRECTIVES
in thedirectives
portion of the@Component
- Initialized
myModel
我是什么在这里失踪?
推荐答案
我认为你应该使用比 ngModel
别的东西为你的参数我选
组件...因为它已经使用Angular2。
I think you should use something else than ngModel
for the parameter of your my-select
component... Because it's already used by Angular2.
我与模式
一个尝试,它似乎更好......我没有错误了。
I made a try with model
and it seems better... I don't have the error anymore.
修改
如果你想在你的我的选
组件的水平来处理ngModel,你可以看看这个链接:的 https://github.com/angular/angular/issues/2543 。
If you want to handle ngModel at the level of your my-select
component, you could have a look at this link: https://github.com/angular/angular/issues/2543.
如果你想实现一个符合ngModel组件,你可以看看下面的链接:
If you want to implement an ngModel-compliant component, you could have a look at the following links:
- 的http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/.参见:NgModel兼容设备
- Angular 2自定义表单输入
- http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/. See section: NgModel-compatible component
- Angular 2 custom form input
希望它可以帮助你,
蒂埃里
Hope it helps you, Thierry
这篇关于ngModel:无值访问器''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!