ngModel:无值访问器'' [英] ngModel: No value accessor for ''

查看:151
本文介绍了ngModel:无值访问器''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的角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:


  1. 导入 {FORM_DIRECTIVES} 'angular2 /通用

  2. 包括 FORM_DIRECTIVES 指令部分中的 @Component

  3. 初始化基于myModel

  1. Imported {FORM_DIRECTIVES} from 'angular2/common'
  2. Included FORM_DIRECTIVES in the directives portion of the @Component
  3. 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/. See section: NgModel-compatible component
  • Angular 2 custom form input

希望它可以帮助你,
蒂埃里

Hope it helps you, Thierry

这篇关于ngModel:无值访问器''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆