gpt4 book ai didi

javascript - 未触发 Angular 4 单击按钮功能

转载 作者:行者123 更新时间:2023-11-30 11:20:46 25 4
gpt4 key购买 nike

我正在尝试检查文本输入是否为空或 Angular 4。我没有为此使用表单。它只是一个输入字段。当我在下面的按钮中执行 addLocaton 函数时,需要进行检查。

my input field

<ion-input type="text" placeholder="Enter a Town, Province or City" name="newPlace" [(ngModel)]="newPlace" id="empty"></ion-input>

<button ion-button block type="submit" (click)="addLocation()">ADD AND SAVE MORE LOCATIONS</button>

ts file

  addLocation(){
if( document.getElementById('empty').value === '' ){
alert('empty');
}
}

最佳答案

正确的方法

根据@edkeveked 的建议,将type="button" 改为type="submit"

 <button ion-button block type="button" (click)="addLocation(newPlace)">ADD AND SAVE MORE LOCATIONS</button>

但是你可以通过方法而不是使用纯 JavaScript 代码来传递模型对象

addLocation(newPlace){
if(newPlace === '' || newPlace === undefined ){
alert('invalid');
}
}

或者使用双向绑定(bind)

(点击)="addLocation()"

和 ts 可能是

addLocation(){
if(this.newPlace === '' || this.newPlace === undefined ){
alert('invalid');
}
}

关于javascript - 未触发 Angular 4 单击按钮功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com