简体   繁体   中英

Angular2: Exception “No provider for t!” when creating a form

<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>

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

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

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">

Or add the NgForm directive:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM