Angular 6中的ngModel在appcomponent中没有改变值。
创始人
2024-10-16 11:00:44
0

问题描述: 在Angular 6中,ngModel在AppComponent中没有改变值。

解决方法:

  1. 确保在使用ngModel之前已经导入了FormsModule。在app.module.ts文件中添加以下代码:

    import { FormsModule } from '@angular/forms';
    
    @NgModule({
      imports: [
        FormsModule
      ],
      //...
    })
    
    export class AppModule { }
    
  2. 在AppComponent中,确保使用了双向绑定语法[(ngModel)]并且与一个变量绑定。例如:

    
    
  3. 在AppComponent类中,确保已经定义了myVariable属性,并且可以进行修改。例如:

    export class AppComponent {
      myVariable: string;
    
      //...
    }
    
  4. 如果在子组件中使用ngModel,并且希望将其更改的值传递回AppComponent,则可以使用@Output装饰器和EventEmitter来实现。首先,在子组件中定义一个@Output属性,并创建一个EventEmitter实例。在子组件中更改ngModel的值时,触发该事件。然后,在AppComponent中监听该事件并处理传递的值。例如: 子组件:

    import { Component, EventEmitter, Output } from '@angular/core';
    
    @Component({
      selector: 'app-child',
      template: `
        
      `
    })
    
    export class ChildComponent {
      @Output() valueChange = new EventEmitter();
      childValue: string;
    
      onChildValueChange() {
        this.valueChange.emit(this.childValue);
      }
    }
    

    AppComponent:

    
    
    export class AppComponent {
      myVariable: string;
    
      onChildValueChange(value: string) {
        this.myVariable = value;
      }
    }
    

通过以上步骤,应该能够在AppComponent中正确地改变ngModel的值。

相关内容

热门资讯

2分钟辅助!hhpoker透视... 2分钟辅助!hhpoker透视脚本,wepoker有透视吗,教程书教程(有挂头条)1、超多福利:超高...
第七分钟辅助!wepoker透... 第七分钟辅助!wepoker透视app下载,aa poker辅助包,讲义教程(有挂分享)一、wepo...
第八分钟辅助!wepoker怎... 第八分钟辅助!wepoker怎么开辅助,xpoker辅助器,模块教程(的确有挂)1、不需要AI权限,...
一分钟辅助!wepoker有没... 一分钟辅助!wepoker有没有挂,pokemmo脚本辅助器,讲义教程(有挂教学)1、完成pokem...
第一分钟辅助!hhpoker是... 第一分钟辅助!hhpoker是真的还是假的,wepoker辅助器,阶段教程(有挂技巧)1、金币登录送...
8分钟辅助!hhpoker辅助... 8分钟辅助!hhpoker辅助挂,wejoker私人辅助软件,窍要教程(有挂分析)1、玩家可以在we...
第6分钟辅助!xpoker透视... 第6分钟辅助!xpoker透视辅助,wejoker内置辅助,窍门教程(有挂秘诀)该软件可以轻松地帮助...
第六分钟辅助!aapoker公... 第六分钟辅助!aapoker公共底牌,aapoker怎么设置抽水,办法教程(真是有挂)1、在aapo...
9分钟辅助!wepoker辅助... 9分钟辅助!wepoker辅助真的假的,aapoker透视脚本下载,手册教程(有挂解密)1、首先打开...
第7分钟辅助!wepoker底... 第7分钟辅助!wepoker底牌透视脚本,wepoker黑侠破解,模板教程(揭秘有挂)1、wepok...