Angular 4中的模态形式

时间:2017-06-24 08:53:56

标签: angular ng-bootstrap

假设我的项目中有一个链接打开一个包含注册表单的模态,这个表单由angular验证, 我在我的项目中使用了ng-bootstrap库,在模态中我定义了一个指令,然后为它编写了一个组件,但是模态将不会打开并发生错误:

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

1 个答案:

答案 0 :(得分: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,您也可以尝试。