Angular2:异常"t没有提供者!";创建表格时 [英] Angular2: Exception "No provider for t!" when creating a form

查看:116
本文介绍了Angular2:异常"t没有提供者!";创建表格时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div id="my-form" class="form-inline">
  <div class="form-group">
    <input type="text" [(ngModel)]="name" ngControl="name" #n="ngForm" required>
    <div [hidden]="n.valid" class="alert alert-danger">
      Value is required
    </div>
  </div>
</div>

我在使用alpha.52时收到以下错误(没有帮助):

I get the following (less than helpful) error with alpha.52:

例外:没有提供者t! (t-> t)

EXCEPTION: No provider for t! (t-> t)

推荐答案

Angular使用NgForm在其ngControl名称下注册控件.通常,您无需显式添加NgForm指令,因为它会自动添加–.如果使用<form>元素.但是您没有<form>元素(就Twitter Bootstrap而言,BTW是有效的/精细的……但是Angular却不多).因此,将外部<div>更改为<form>:

Angular registers controls under their ngControl names with the NgForm. Normally, you don't have to add the NgForm directive explicitly because it automatically gets added – if you use a <form> element. But you don't have a <form> element (which BTW is valid/fine as far as Twitter Bootstrap is concerned... but not Angular so much). So, either change the outer <div> to <form>:

<form id="my-form" class="form-inline">

或添加NgForm指令:

Or add the NgForm directive:

<div ngForm id="my-form" class="form-inline">

这篇关于Angular2:异常"t没有提供者!";创建表格时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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