Alcadur
Alcadur

Reputation: 685

angular 2 form local

I have form like:

<form  #testForm="ngForm" (submit)="create(testForm)">
    <input type="text" [value]="111" ng-control="test">
    <input type="submit" >
</form>

but when I console.log testForm in there is no test key (and testForm.value is undefined)

Upvotes: 1

Views: 133

Answers (2)

Zyzle
Zyzle

Reputation: 2280

Two thing, (submit) should be (ngSubmit) and ng-control should be ngControl. Also using [value] to set an initial value wont work, use [(ngModel)] with an initial value instead.

Here's an example: http://plnkr.co/edit/qi08ZTgFWH2WpHWre7qu

Upvotes: 4

TGH
TGH

Reputation: 39278

Try this:

<form (ngSubmit)="create()" [ngFormModel]="form" #testForm="ngForm">
    <input type="text" ngControl="test">
</form>

the form instance in my sample is defined in the component code.

Here is a working form demo/article if you need it as well:

http://www.syntaxsuccess.com/angular-2-samples/#/demo/form

http://www.syntaxsuccess.com/viewarticle/forms-and-validation-in-angular-2.0

Upvotes: 4

Related Questions