angular - ngOnChanges 没有触发
问题描述
changeEnt()
如果我运行方法,我无法弄清楚为什么 ngOnChanges 会被执行,而如果我点击 GoogleMaps 标记则不会被执行。
这是我的父组件
@Component({
selector: 'app-wl-map',
templateUrl: './wl-map.component.html',
styleUrls: ['./wl-map.component.scss']
})
export class WlMapComponent extends BaseComponentComponent implements OnInit {
@Input() entResList: Enterprise[];
@ViewChild('gmap') gmapElement: any;
coords: Coordinates;
map: google.maps.Map;
selectedEnt: Enterprise = new Enterprise();
/**
*
* @type {any[]}
*/
conts = [];
constructor(private router: Router) {
super();
}
ngOnInit() {
this.requestLocation();
}
/**
*
*/
requestLocation(): void {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
// this.initMap(position.coords);
this.coords = position.coords;
});
} else {
alert("Geolocation is not supported by this browser.");
}
}
/**
* init map
*/
initMap(): void {
let lat = this.coords ? this.coords.latitude : DEFAULT_COORDS.latitude;
let lng = this.coords ? this.coords.longitude : DEFAULT_COORDS.longitude;
var mapProp = {
center: new google.maps.LatLng(lat, lng),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(this.gmapElement.nativeElement, mapProp);
this.placeMarers();
}
/**
* position marker
*/
placeMarers(): void {
if (this.entResList) {
var markers: google.maps.Marker[] = this.entResList.map(function (ent: Enterprise, i) {
if (ent.locationList.length > 0) {
let mark = new google.maps.Marker({
position: new google.maps.LatLng(ent.locationList[0].latitudine, ent.locationList[0].longitudine),
label: this.getMarkerLabel(ent)
});
google.maps.event.addListener(mark, 'click', this.markerClickHandler.bind(this, ent));
return mark;
}
}.bind(this));
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(this.map, markers, {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
}
/**
* invoked on marker click
* @param {Enterprise} ent
*/
markerClickHandler(ent: Enterprise): void {
if (ent) {
this.selectedEnt = new Enterprise();
}
}
changeEnt() {
setTimeout(function () {
this.selectedEnt = new Enterprise();
console.log('this button handler', this);
}.bind(this), 1000)
}
}
这是我的父组件模板
<button mat-raised-button color="primary" (click)="initMap()">Init Map</button><br/><br/>
<div class="map-container">
<app-wl-map-popup-enterprise [ent]="selectedEnt"></app-wl-map-popup-enterprise>
<div id="google-map" #gmap style="width:100%; height:400px"></div>
<button (click)="changeEnt()">Change ENt</button>
</div>
这是我的子组件
@Component({
selector: 'app-wl-map-popup-enterprise',
templateUrl: './wl-map-popup-enterprise.component.html',
styleUrls: ['./wl-map-popup-enterprise.component.scss'],
changeDetection: ChangeDetectionStrategy.Default
})
export class WlMapPopupEnterpriseComponent extends BaseComponentComponent implements OnInit, OnChanges {
@Input() ent: Enterprise;
constructor() {
super();
}
ngOnInit() {
console.log('init', this.ent);
console.log('globals', this.globals);
}
ngOnChanges(changes: {[propKey: string]: SimpleChange}) {
console.log('**************************** changes', changes)
}
}
如果我加载地图并单击标记markerClickHandler()
,则会执行,但不会拦截更改。
否则,如果我单击 html 按钮Change ENt
并changeEnt()
执行更改,则会被拦截。
我引入了一个超时来检查问题是否是由于上下文的变化,但似乎不是
解决方案
推荐阅读
- typescript - 在打字稿界面键迭代中使用条件类型
- r - 从 text2vec 获取 LDAvis json
- c# - 等待应用程序启动而不使用 Thread.Sleep() 使用 FLAUI
- sql - WHERE 语句不适用于数字和日期(SQLite)
- powershell - 如何检查 Microsoft Azure KeyVault 是否存在或是否已成功创建?
- c++ - 在 C MexFunction 中使用 Besselk 函数
- c# - WCF 服务仅接收 65536 String.Length 大小,不超过
- error-handling - 导入 PyQt5 模块时出现问题,程序崩溃:进程以退出代码 -1073741819 (0xC0000005) 完成
- bash - 使用列表操作 bash 脚本字符串
- php - 将xml简单元素对象读入数组