2

My problem is when I change input field value using javascript, it won't change in ngModel value.

For example:

//html 
<input type="text" name="date" [(ngModel)]="date">

Typescript

  var date : any;
    constructor(
            // some code
        ) {
            this.date="10:10:2016";
        }

When I console 'date' it will show 10:10:2016

After executing this Javascript code

document.getElementsByName("date")[0].value = "15:10:2016";

text field will change its value, but ngModel value won't change

btw I use jQuery datepicker

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nandhu
  • 339
  • 4
  • 14

2 Answers2

1

You need to trigger an input event on the element so Angular knows that something changed. Look at the answer here: Change AngularJS input text value using javascript

Community
  • 1
  • 1
sloppypasta
  • 1,068
  • 9
  • 15
1

you need to execute the javascript code in ngAfterViewInit

ngAfterViewInit() {  
  let myNewDate= document.getElementsByName("date")[0].value = "15:10:2016";; 
  this.date = myNewDate;
}

and assign the value to your date variable.

candidJ
  • 4,289
  • 1
  • 22
  • 32
  • Thanks , this code works fine . but i use external js (jquery) to assign value to input field and i think i found a solution.. – Nandhu Jan 10 '17 at 04:53
  • Use jquery input function in ngView and assign value of input field value to ngModel now i don't have access to my pc to test this code. Will post solution after testing :) , your answer helped me to solve my problem .. thank you – Nandhu Jan 10 '17 at 05:51