Ironsun
Ironsun

Reputation: 881

value not updating in the view after data update , angular2 and typescript

this is my ngOnInit function

 instance.input = document.getElementById('google_places_ac');
        autocomplete = new google.maps.places.Autocomplete(instance.input, { types: ['(cities)']});
        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            var place = autocomplete.getPlace();
            instance.setValue(place.address_components[3].long_name, place.address_components[2].long_name, place.address_components[1].long_name);

         });

and this is my setValue() function

public setValue(a,b,c) {
    this.coutnry = a;
    this.state = b;
    this.city = c;
    this.sharedService.country = this.coutnry;
    this.sharedService.city = this.city;
    this.sharedService.state = this.state;
    console.log(a, b, c);
}

and below is my html template to show and get the values

 <input id="google_places_ac" attr.state="{{state}}" attr.country="{{coutnry}}" name="google_places_ac" type="text" value="{{city}}" class="form-control" />

the problem is my html attributes not getting updated once place_changed function is called,is there anything i have to do to get updated values on view?

Upvotes: 1

Views: 2432

Answers (2)

micronyks
micronyks

Reputation: 55443

you can check this angular2 official docs for setting up the attributes,

cheatsheet - https://angular.io/docs/ts/latest/cheatsheet.html

<input id="google_places_ac" [attr.state]="state" [attr.country]="coutnry"
name="google_places_ac" type="text" value="{{city}}" class="form-control" />

Update

import { Component, ChangeDetectorRef } from 'angular2/core';

constructor(private cdr:ChangeDetectorRef) {
}
public setValue(a,b,c) {
    this.coutnry = a;
    this.state = b;
    this.city = c;
    this.sharedService.country = this.coutnry;
    this.sharedService.city = this.city;
    this.sharedService.state = this.state;
    console.log(a, b, c);
    this.cdr.detectChanges();
}

Upvotes: 3

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

You should use property binding by wrapping attribute inside []

<input id="google_places_ac" [attr.state]="state" [attr.country]="coutnry" 
 name="google_places_ac" type="text" [value]="city" class="form-control" />

Additional thing is, you made typo while writing country like you wrotecoutnry instead in two places.

this.sharedService.country = this.coutnry; //in constructor

attr.country="{{coutnry}}" //second place

Upvotes: 1

Related Questions