【问题标题】:Angular2 input not triggering change eventAngular2输入未触发更改事件
【发布时间】:2017-10-10 23:00:41
【问题描述】:

我有一个正在从查询更新的输入,但是,更新输入字段时不会触发(更改)事件。

输入的HTML:

<input type="text" [ngModel]="inputValue" id="itemText" (ngModelChange)="setNewItem($event)">

用于输入的组件文件中的函数:

setNewItem(item){
console.log("Triggered");
return this.ds.setItem(item);  }

我正在像这样更新索引 html 中的输入字段:

 <script>
 ....
 onSelect: function (request, response) {
        var urlApi = 'http://localhost:8088/Badges/Details/' + request.item;
                   $.ajax({
                    type: "GET",
                    url: urlApi,
                    dataType: 'json',
                    success: function(response) {
                      document.getElementById("itemText").value = response.items[0].item; } 
     }); 
}
</script>

【问题讨论】:

  • 使用 [(ngMode)l]="inputValue" 而不是 [ngModel]="inputValue"
  • 我没有发现你的代码有任何问题。它应该工作。检查控制台是否有错误,可能与此无关。
  • 这也不会触发事件,但是,你是对的,我应该使用 2 路绑定,谢谢
  • @Muthukumar 控制台也没有显示任何错误,但是当我直接在输入字段中输入时,事件被触发
  • @TestNInja “但是当我直接在输入字段中输入时,事件被触发” - 我以为你在抱怨事件没有触发。我错过了什么吗?

标签: angular typescript events


【解决方案1】:

你不应该这样做,

document.getElementById("itemText").value = response.items[0].item;

而是尝试将值直接分配给绑定属性(应该在组件级别完成,以便访问inputValue),

 this.inputValue = response.items[0].item;

并使用 2-way 绑定语法。

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多