如果元素具有(click)
处理程序并且您在此元素中选择了一些文本,则会调用(click)
处理程序。有办法防止这种情况吗? Here's a Plunkr说明了问题。相关代码:
@Component({
selector: 'my-app',
template: `
<div>
<h2 (click)="handleClick()">Click or select this text</h2>
</div>
`
})
export class App {
public handleClick() {
alert('you clicked');
}
}
答案 0 :(得分:5)
使用此代码
public handleClick() {
var selection = window.getSelection();
if(selection.toString().length === 0) {
alert('you clicked');
}
}
答案 1 :(得分:2)
将点击事件($event
)传递给handleClick
方法
@Component({
selector: 'my-app',
template: `
<div>
<h2 (click)="handleClick($event)">Click or select this text</h2>
</div>
`
})
然后使用以下代码获取选择类型:
export class App {
public handleClick(event) {
if (event.view.getSelection().type !== 'Range') {
alert('you clicked');
}
}
}
或:
export class App {
public handleClick(event) {
if (event.view.getSelection().toString().length === 0) {
alert('you clicked');
}
}
}
Plunker演示:https://plnkr.co/edit/PEPyPzDkFjweyAHK8Tj6?p=preview