我有一个父函数ngOnInit()
,它从谷歌地图获取值如下
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);
});
setValue()
是与共享服务共享价值的功能,在 html 页面上我与父母和孩子有同样的事情
<input id="google_places_ac" [(attr.state)]="state" [(attr.country)]="coutnry" name="google_places_ac" type="text" value="{{city}}" class="form-control" />
setValue()
在父组件类中,我在函数上触发 changedetection
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;
this.cdr.detectChanges();
// console.log(this.coutnry, this.state, this.city);
}
这在父级上运行良好,但在子级上没有发生变化,我创建了一个点击功能,它在子级上触发 changedetection 也可以,但我希望它从父级自动触发有什么解决方法吗?