简体   繁体   中英

How to access default value from template driven form of angular

I have Created a template driven form in angular. And in that form for one input field I am getting value from another component. And I have assigned that value to the input field. I have created model also. And now I need to access that value after clicking ok button. In console I am getting empty value. How to get that dynamic value. Any help please

form.html

    <form name="form" #f="ngForm" class="form-horizontal">



         <div class="form-row">

            <label>Remaining qty: &nbsp;&nbsp;</label>

           <div class="form-group col-md-3">
             <input type="text" [value]="myValue"  class="form-control" [(ngModel)]="myModel.modelValue.value"  name="modelValue" readonly>
          </div>
        </div> 

  <button type="button" class="btn btn-primary" (click)="submit()">OK</button>
    </form>

form.ts

import { Component, OnInit ,Input} from '@angular/core';
import { MyModel } from './myModel.model';


@Component({
  selector: 'app-form,
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {

    constructor(){
    }

  myModel: MyModel= new MyModel();
   rowData:any;

  @Input() myValue:any;


  ngOnInit() {
  }


  submit(){

    console.log(this.myModel);

}

form.model

import { BasicSearchModelI, BasicSearchKey } from '../base';

    export class myModel{

          modelValue: BasicSearchModelI = {
                value: '',
                apiKey: 'modelValue'
          };


    }

You are assigning value attribute as well as doing two way binding, that will not work. instead of this you can assign myValue in myModel.modelValue.value object in ngOnInit() method and just do two way binding as shown below

form.html

<input type="text" class="form-control" [(ngModel)]="myModel.modelValue.value" name="modelValue" readonly>

form.ts

ngOnInit()
{
     myModel.modelValue.value = myValue;
}

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