【问题标题】:ngModel is not working while update in tag-InputsngModel 在标签输入中更新时不起作用
【发布时间】:2019-08-27 04:12:23
【问题描述】:

使用 ngModel 的双向数据绑定在标签输入中不起作用。有人可以帮忙吗?

https://stackblitz.com/edit/angular-9muqcy?file=src%2Fapp%2Fapp.component.html

【问题讨论】:

  • 数组更新后工作正常
  • 在哪里,不,我使用控制台检查过。它给出了未定义的
  • 错误是什么?
  • 我要在控制台中显示的标签输入中更新的内容。但它给出了未定义的。
  • 你控制台记录 this.hi

标签: angular


【解决方案1】:

只要改变这个:

hai(){
console.log(this.hi)
}

到这里:

hai(){
console.log(this.Myarray)
}

查看此链接:https://stackblitz.com/edit/angular-czsxvq

【讨论】:

  • 哦,是的,你是对的。我没有检查整个数组。只是我正在检查 ngModel 值。无论如何谢谢你。
  • 从第三个标签显示在对象中。我需要这样的 [手机、笔记本电脑、ThirdTag]。
【解决方案2】:

当您的 ngModel 变量为 Myarray 时,您尝试显示 hi

【讨论】:

    【解决方案3】:

    新编辑 -

    hai(){
       let currItem = this.Myarray[this.Myarray.length-1]['value'];
       this.Myarray.splice(this.Myarray.length-1);
       this.Myarray.push(currItem);
       console.log(this.Myarray)
      }
    

    您收到此错误是因为未定义“hi”,只是声明了它。这个帖子很好解释。

    What's the difference between variable definition and declaration in JavaScript?

    你需要编辑你的 hai 函数。

    hai(){ console.log(this.Myarray) }

    也添加这个

    import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
    
    @NgModule({
      imports:      [ BrowserModule, FormsModule, TagInputModule,BrowserAnimationsModule ],
    

    【讨论】:

    • 谢谢你,但是,在数组中我已经有两个标签,一个是手机,另一个是笔记本电脑。当我输入第三个标签时,它给出了这样的结果。 [手机、笔记本电脑、对象]。但我需要 [手机、笔记本电脑、thirdtag]
    • 这里我需要我输入的所有标签需要显示的内容。你只显示长度 1,我的意思是只有最后一个标签。请查看上面的 stackblitz 链接。我有 ["mobiles","laptops"] 默认值。如果我输入了'n'个标签,所有标签都需要以这种格式显示--->["mobiles","laptops","x","y","z"...].
    • 感谢您的回复。最后我得到了结果。 ---->hai(){ const mynewArr = this.Myarray.map(item => typeof item == "string"? item : item.value) console.log(mynewArr) }
    • 我写的代码应该和你说的完全一样。它以 ["mobiles","laptops","x","y","z"...] 格式显示数组的所有值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多