1

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?

2 Answers 2

3

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();
}
Sign up to request clarification or add additional context in comments.

9 Comments

i changed console.log to console.log(this.coutnry, this.state, this.city); this its showing proper values but no update on the view
This could be changeDetection problem or ngZone problem. Check my udpdated answer.
yes dude... thank a lot for enlightening me with changedetection :)
hey i just got a follow up problem , how can i detect changes on child element from parent , the event is fired from parent component and child component is in route-outlet getting value from shared service , can you help me?
@micronyks haven't look at code..as it has been fired from outer context of A2.. +1 for core problem of change detection
|
1

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

8 Comments

when i do that i get thi long error Got interpolation ({{}}) where expression was expected at column 0 in [{{state}}] and this for all attrs. i am calling this function in ngOnInit
@Ironsun are you sure you change you code to my udpated answer?
Eagle eyes @PankajParkar
ok i was using {{}} but now not getting the error but still values not updating
@Ironsun could you debug and confirm that setValue function is setting state & other value correctly?
|

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.