2017-06-24 76 views
0

假设我在我的项目中有一个链接,打开一个模态包含注册表单,并且此表单通过角度验证, 我在我的项目中使用ng-bootstrap库并在模式中定义了一个指令,然后编写一个指令对于它,但模态组件将不会被打开,发生错误:Modular Form in Angular 4

ERROR Error: No provider for Router! 
    at injectionError (core.es5.js:1169) 
    at noProviderError (core.es5.js:1207) 
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._throwOrNull (core.es5.js:2649) 
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._getByKeyDefault (core.es5.js:2688) 
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._getByKey (core.es5.js:2620) 
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_.get (core.es5.js:2489) 
    at resolveNgModuleDep (core.es5.js:9562) 
    at NgModuleRef_.webpackJsonp.../../../core/@angular/core.es5.js.NgModuleRef_.get (core.es5.js:10644) 
    at resolveDep (core.es5.js:11147) 
    at createClass (core.es5.js:11011) 

app.component.html:

<ng-template #signup let-c="close" let-d="dismiss"> 
     <div class="modal-header"> 
      <button type="button" class="close" aria-label="Close" (click)="d('Cross click')"> 
       <span aria-hidden="true">&times;</span> 
      </button> 
     </div> 
     <div class="modal-body"> 
      <div class="row"> 
       <div class="col-6"></div> 
       <div class="col-5 mt-5 mb-2"> 
        <div class="wrapper-left-content"> 
         <app-register-form></app-register-form> 
        </div> 
       </div> 
      </div> 
     </div> 
    </ng-template> 

register.compon ent.ts:

import { Component, Injectable, OnInit } from '@angular/core'; 
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; 
import { Router } from '@angular/router'; 
import { AuthService } from '../services/auth.services'; 
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; 

@Component({ 
    selector: 'app-register-form', 
    templateUrl: '../templates/register.component.html' 
}) 
@Injectable() 
export class RegisterComponent implements OnInit { 
    private errorMessage: any ; 
    closeResult: string; 

    FirstName = new FormControl('', [ 
     Validators.required 
    ]); 
    LastName = new FormControl('', [ 
     Validators.required 
    ]); 

    Password = new FormControl('', [ 
     Validators.required, 
     Validators.minLength(6) 
    ]); 
    Password_Confirm = new FormControl('', [ 
     Validators.required 
    ]); 

我该如何解决它?

我调用模式这段代码:

<div class="user-info"> 
    <div class="user-login-link"> 
     <a href="#" data-toggle="modal" data-target="#signup" (click)="open(signup)"></a> 
    </div> 
    <div class="user-login-link"> 
     <a href="#" data-toggle="modal" data-target="#signin" (click)="open(signin)"></a> 
    </div> 
</div> 
+0

您是否尝试使用Angular的'* ngIf'指令显示/隐藏? – Abrar

+0

我不认为这与modals或ng-bootstrap有任何关系。您似乎在某处(某处)使用路由器服务,但未在根NgModule中导入RouterModule。 –

+0

我编辑我的问题 – mosi

回答

0

No provider for Router意味着你缺少这样的定义:

//For the app.module.ts 
@NgModule({ 
    imports: [RouterModule.forRoot(...)] 

//For other modules 
@NgModule({ 
    imports: [RouterModule.forChild(...)], 

另外,还要确保你正在使用它的最新版本。我的意思是第三方组件。 还有其他好的组件,例如https://github.com/valor-software/ngx-bootstrap,你也可以试试。

+0

因为你的回答是误导性的,所以我投下了票:问题是关于https://ng-bootstrap.github.io/#/components/modal,它与'ngx-bootstrap'没有关系 - 它们是2个独立的项目,并且绝不会重命名!请删除错误的信息信息,我可以删除向下投票。 –