3

here i am using a typical input scenario where i can't use reactive/template form structure and ngmodel already assigned to other task. now i want to set a default value to the input fields and if user wants he can change the change

is it possible

<input id="{{data.name}}" value="{{data.name}}"  type="{{data.type}}" [(ngModel)]="Edit[data.name]" class="form-control">

mainly my issue setting value to the input fields & if user wants then he may change value to the individual fields or setting value to dynamic ngmodel

2
  • [(ngModel)]=" data.name" Doing so you can set the value Commented Oct 4, 2018 at 5:44
  • " and ngmodel already assigned to other task. now i want to set a default value". The only way I know is give value to your variable "Edit[data.name]" (well I supouse yo have an object data with default values) You can use placeholder to see a message like "enter name" Commented Oct 4, 2018 at 6:40

5 Answers 5

2

Try this:

<input type="text"  [value]="data.name"/>

if you still need to way binding

<input type="text"  [value]="test"  [(ngModel)]="test"/>
Sign up to request clarification or add additional context in comments.

6 Comments

that didn't work for me and that why for setting value trying any alternative approach
did you try with the [] ? in your question you have with without the brackets and using {{}}
like i said ngmodel is already being used by another task and that to it is dynamic model [(ngModel)]="Edit[dataname]" i cant set value to this right
if you want to set a value in the text without using anything else but the input field without using [(ngModel)] i dont think it can be done since ngModel is what makes the 2 way binding for us. If you can use like a button or something else then sure. Like an event or a button or something else @Dexter
i found a way using the template reference and view child i can set value
|
0

using reference variable #inp

  OnInputClick(inp:any){   }//Typesctipt
<input id="{{data.name}}" value="{{data.name}}"  type="{{data.type}}" #inp class="form-control">
<button (click)="OnInputClick(inp.value)">Input</button>

1 Comment

the fields are dynamic and i am not getting values i m setting the value
0

use keyup to capture event and store in .ts

 <input (keyup)="onKey($event)">

in .ts

"event.target.value"

have value entered by user , use it as you want

values ='' // provide a default value here whatever you want
onKey(event: any) { 
    this.values += event.target.value;
  }

3 Comments

have to set value please refer to my question
thats why defined values = "your value " in .ts
you can use {{values}} in your html displaying its result , if you want to display this in your input tag then there are attributes for input tag like [value] where you can assign this variable to display some value in input field. eg [value]= "values" // [value] an attribute and values a var defined in .ts.
0

As per then Angular Document you can use the following code

<input  [value]="data.name" (input)="data.name=$event.target.value">

Comments

0

You could also use an on blur event

This updates the value whenever the user leaves the input field you started to edit.

example.html:

<input #tsVarName (blur)="addBlurVal(tsVarName.value)" >

example.ts

export class example {
    globalVal = "";

    addBlurVal(localVar) {
         this.globalVal = localVal;
    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.