简体   繁体   English

TypeError:无法读取 null 的属性(读取“writeValue”)

[英]TypeError: Cannot read properties of null (reading 'writeValue')

I'm trying to get the object by id, but if that object doesn't exist, the component will register the object.我试图通过 id 获取对象,但如果该对象不存在,组件将注册该对象。

but when the object exists, it takes the object normally and throw this error again但是当对象存在时,它会正常获取对象并再次抛出此错误

HTML: HTML:

    <div class="card shadow-sm m-5 p-5 bg-light" style="min-width: 60%; max-width: 70%">
      <mat-toolbar>{{(veiculo && veiculo.id)?'Editar Veiculo':'Cadastrar Veiculo'}}</mat-toolbar>
    
      <div class="card-body">
          <div class="form-group" *ngIf="veiculo.id != null">
            <label class="form-label">ID</label>
            <input type="text" class="form-control" [(ngModel)]="veiculo.id" readonly />
          </div>
          <div class="form-group">
            <label class="form-label">Placa Oeste</label>
            <input type="text" class="form-control" [(ngModel)]="veiculo.placaOeste" />
          </div>
          <div class="form-group">
            <label class="form-label">Modelo</label>
            <input type="text" class="form-control" [(ngModel)]="veiculo.modelo">
          </div>
          <div class="form-group">
            <label class="form-label">Marca</label>
            <input type="text" class="form-control" [(ngModel)]="veiculo.marca">
          </div>
          <div class="form-group">
            <label class="form-label">Ano</label>
            <input type="text" class="form-control" [(ngModel)]="veiculo.ano">
          </div>
          <div class="form-group form-check">
            <input type="checkbox" class="form-check-input">
            <label class="form-check-label" [(ngModel)]="veiculo.portariaSEJUSP">Deriva da Portaria SEJUSP/DGPC nº
        133/2018</label>
    </div>
    
          <div>
            <button class="btn btn-primary m-2" (click)="salvar()">
              Salvar
            </button>
            <button type="button" class="btn btn-warning mx-2" routerLink="/veiculos">
              Cancelar
            </button>
          </div>
      </div>

TS: TS:

export class VeiculoCadastrarEditarComponent implements OnInit {
  veiculoForm!: FormGroup;
  veiculo = new Veiculo();
  id: any = 0;
  constructor(
    private service: VeiculoService,
    private router: Router,
    private activatedRoute: ActivatedRoute
  ) {}

  ngOnInit(): void {
    this.id = this.activatedRoute.snapshot.paramMap.get('id');
    if (this.id) {
      this.service.pesquisarPorId(this.id).subscribe({
        next: (data) => (this.veiculo = data),
        error: (err) => console.log(err),
      });
    }
  }

  salvar() {
    if (this.veiculo && this.veiculo.id) {
      this.service
        .atualizar(this.veiculoForm.value, this.veiculo.id)
        .subscribe({
          next: () => this.router.navigateByUrl('/veiculos'),
          // error: () => this.onErrorEdicao(),
        });
    } else {
      this.service.cadastrar(this.veiculoForm.value).subscribe({
        next: () => this.router.navigateByUrl('/veiculos'),
        // error: () => this.onErrorCadastro(),
      });
    }
  }

  deletar() {
    this.service.deletar(this.veiculoForm.value).subscribe({
      next: () => this.router.navigateByUrl('/veiculos'),
      // error: () => this.onErrorDelete(),
    });
  }
}

INFO:信息:

Angular CLI: 14.0.5 Node: 16.14.2 Package Manager: npm 8.5.0 OS: win32 x64 Angular CLI:14.0.5 节点:16.14.2 包管理器:npm 8.5.0 操作系统:win32 x64

EDIT:编辑:

I found that the error is in the html checkbox, the error goes out when I take it out but I need it, how can I solve it?我发现错误在html复选框中,取出时错误消失但我需要它,我该如何解决?

The attribute of checkbox is boolean复选框的属性是布尔值

我猜错误是 - [(ngModel)] 应该在输入标签中给出,而不是在标签标签中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”) - Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue') 类型错误:无法读取 null 的属性(读取“数量”) - TypeError: Cannot read properties of null (reading 'quantity') 类型错误:无法读取 null 的属性(读取“文件名”) - TypeError: Cannot read properties of null (reading 'fileName') TypeError:无法读取 null 的属性(读取“_rawValidators”) - TypeError: Cannot read properties of null (reading '_rawValidators') 错误类型错误:无法读取 null 的属性(读取“toISOString”) - ERROR TypeError: Cannot read properties of null (reading 'toISOString') angular 错误类型错误:无法读取 null 的属性(读取“_rawValidators”) - angular ERROR TypeError: Cannot read properties of null (reading '_rawValidators') 错误类型错误:无法读取 null 的属性(读取“拆分”) - ERROR TypeError: Cannot read properties of null (reading 'split') TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11 - TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11 TypeError:无法读取 null 的属性(读取“获取”)FormGroup / FormArray - TypeError: Cannot read properties of null (reading 'get') FormGroup / FormArray 类型错误:无法读取未定义的属性(读取“拆分”)” - TypeError: Cannot read properties of undefined (reading 'split')"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM