我正在尝试在Angular 2中设置一个蓝色事件,如下所示:
<div id="area-button" (blur)="unfocusAreaInput()" class="form-group" (click)="focusAreaInput()">
component.ts:
import { Component, ViewChild, ElementRef, Output, EventEmitter } from '@angular/core';
import { GoogleplaceDirective } from 'angular2-google-map-auto-complete/directives/googleplace.directive';
@Component({
selector: 'my-area',
directives: [GoogleplaceDirective],
templateUrl: 'app/find-page/area-picker.component.html',
styleUrls: ['app/find-page/area-picker.component.css']
})
export class AreaComponent {
public address: Object;
@ViewChild('areaInput') areaInput;
areaSelected: boolean = false;
@Output() onAreaSelected = new EventEmitter<boolean>();
@Output() onAreaDeselected = new EventEmitter<boolean>();
constructor(el: ElementRef) { }
getAddress(place: Object) {
this.address = place['formatted_address'];
var location = place['geometry']['location'];
var lat = location.lat();
var lng = location.lng();
console.log("Address Object", place);
}
focusAreaInput() {
this.areaInput.nativeElement.focus();
this.onAreaSelected.emit(true);
}
unfocusAreaInput() {
this.onAreaSelected.emit(false);
}
}
unfocusAreaInput()永远不会执行。为什么?
您的blur
活动无法正常进行,因为您一开始div
就无法获得关注。如果您在中添加tabindex="1"
(可以用任何数字替换1)或contentEditable
(这将使div的内容可编辑)到您的div
,它将能够获得焦点,然后blur
事件将起作用。另外,您可以使用focus
代替click
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句