Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

forms - Angular 2: 'ngFormModel' since it isn't a known native property

My error is

 EXCEPTION: Error: Uncaught (in promise): Template parse errors:
Can't bind to 'ngFormModel' since it isn't a known native property ("
<h3 class = "head">MY PROFILE</h3>

<form  [ERROR ->][ngFormModel]="form" (ngSubmit)="onSubmit(form.value)">

 <div class="row">
"): a@3:7
There is no directive with "exportAs" set to "ngForm" ("stname</label>
        <input type="text" id="facebook" class="form-control"  ngControl="firstname" [ERROR ->]#firstname="ngForm" >  
   </div>

"): a@9:85
There is no directive with "exportAs" set to "ngForm" ("/label>
        <input type="text" id="facebook" class="form-control col-xs-3" ngControl="lastname" [ERROR ->]#lastname="ngForm" >  
    </div>

My template,

<h3 class="head">MY PROFILE</h3>

<form [ngFormModel]="form" (ngSubmit)="onSubmit(form.value)">

    <div class="row">

        <div class="form-group">
            <label class="formHeading">firstname</label>
            <input type="text" id="facebook" class="form-control" ngControl="firstname" #firstname="ngForm">
        </div>

        <div *ngIf="firstname.touched">
            <div *ngIf="!firstname.valid" class="alert alert-danger">
                <strong>First name is required</strong>
            </div>
        </div>


        <div class="form-group">
            <label class="formHeading">lastname</label>
            <input type="text" id="facebook" class="form-control col-xs-3" ngControl="lastname" #lastname="ngForm">
        </div>

        <div *ngIf="lastname.touched">
            <div *ngIf="!lastname.valid" class="alert alert-danger">
                <strong>Last name is required</strong>
            </div>
        </div>


        <div class="form-group">
            <label class="formHeading">Profilename</label>
            <input type="text" id="facebook" class="form-control col-xs-3" ngControl="profilename" #profilename="ngForm">
        </div>



        <div class="form-group">
            <label class="formHeading">Phone</label>
            <input type="text" id="facebook" class="form-control col-xs-3" ngControl="phone" #phone="ngForm">
        </div>

        <div *ngIf="phone.touched">
            <div *ngIf="!phone.valid" class="alert alert-danger">
                <strong>Phone number is required</strong>
            </div>
        </div>

        <label class="formHeading">Image</label>
        <input type="file" name="fileupload" ngControl="phone">

        <div class="form-row btn">

            <button type="submit" class="btn btn-primary  " [disabled]="!form.valid">Save</button>
        </div>

    </div>
</form>

My Component

import {Component} from '@angular/core';
import { Http, Response, Headers} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {contentHeaders} from '../headers/headers';
import {FORM_DIRECTIVES} from '@angular/forms';
import {Router, ROUTER_DIRECTIVES} from '@angular/router';
import {Control, FormBuilder, ControlGroup, Validators} from '@angular/common';

@Component({
    templateUrl: './components/profile/profile.html',
    directives: [ROUTER_DIRECTIVES, FORM_DIRECTIVES],
})

export class Profile {

    http: Http;

    form: ControlGroup;

    constructor(fbld: FormBuilder, http: Http, public router: Router) {
        this.http = http;
        this.form = fbld.group({
            firstname: ['', Validators.required],
            lastname: ['', Validators.required],
            profilename: ['', Validators.required],
            image: [''],
            phone: [''],

        });

    }


    onSubmit(form: any) {

        console.log(form);
        let body = JSON.stringify(form);
        var headers = new Headers();
        this.http.post('http://localhost/angular/index.php/profile/addprofile', body, {
                headers: headers
            })
            .subscribe(
                response => {
                    if (response.json().error_code == 0) {
                        alert('added successfully');
                        this.router.navigate(['/demo/professional']);
                    } else {
                        alert('fail');

                    }

                });
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that you're still importing from common and especially using the instructions of the old forms. Import correctly the components for new forms:

import {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

And correct the component:

@Component({
    ...
    directives: [FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES]
})
export class AppComponent {

    form: FormGroup;

    constructor(fbld: FormBuilder) {

        this.form = fbld.group({
            ...

        });


    }

    ...

}

Then correct the view: ngFormModel has been replaced by formGroup, and use formControl for your fields:

<form  [formGroup]="form" (ngSubmit)="onSubmit(form.value)">

    <div class="row">

        <div class="form-group">     
            <label class="formHeading">firstname</label>
            <input type="text" id="facebook" class="form-control" [formControl]="form.controls['firstname']" >  
        </div>

        <div *ngIf ="form.controls['firstname'].touched">
            <div *ngIf ="!form.controls['firstname'].valid" class = "alert alert-danger">
            <strong>First name is required</strong>
            </div>  
        </div>

        ... 

        <div class="form-row btn">

            <button type="submit" class="btn btn-primary" [disabled]="!form.valid">Save</button>

        </div>

    </div>
</form>

Edit. From Angular 2.0.0-rc.5, is necessary to remove the directives from the component and import the form modules in AppModule:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
    imports: [
        ...
        FormsModule,
        ReactiveFormsModule
    ],
    ...
    bootstrap: [AppComponent]
})

export class AppModule { }

If you use a shared module, do not forget to export them:

@NgModule({
    imports: [
        ...
        FormsModule,
        ReactiveFormsModule
    ],
    exports: [
        ...
        FormsModule,
        ReactiveFormsModule
    ]
})

export class SharedModule { }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...