在angular 6表单构建器中找不到具有未指定名称属性的控件

时间:2018-12-18 11:05:35

标签: angular angular6 angular-forms

当尝试使用HTML文件更新网格表时,我收到此错误消息。

在这里,我使用了静态数据表来显示数据,并从其他显示primng表的组件中导入了数据,并添加了带有功能的更新按钮,该功能重定向到另一个页面以更新数据。

问题出现在HTML文件的第一行,即[formGroup] =“ myvehicle”

我尝试使用其他表单组名称进行检查,但问题仍然相同。

  <div>          
        <?php
        $start = "SELECT * FROM plot_points where sub='strat'";
        $query0 = mssql_query($start);
        $row0 = mssql_fetch_array($query0);
        if ($query0 > 0 && $row0['postion'] == '0') {
            ?>
            <!--                <div class="start">
                                <img  src="img/tri.jpg" alt="" style="float: left;" />
                                   <div class="" id="progressbar1"  style=" float: left;width:100px; padding-top: 15px; margin-left: -15px;   "></div>                       
                            </div>-->


            <?php
        }
        $sql = "SELECT * FROM plot_points ";
        $query = mssql_query($sql);
        echo '<div class="start">';
        $i = 1;
        $top = 0;
        $ml = 70;


        while ($row = mssql_fetch_array($query)) {
            if ($row['sub'] == 'strat') {
                ?>
                <span style=" position: fixed; font-size: 13px; margin-top: -20px;"><?php echo $row['points']; ?></span>

                <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/start.png" alt="" style="float: left; height: 50px; width:50px;" />

                <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; padding-top: 20px;   margin-left: 0px;   "></div>                       
                <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px;margin-top:10px;   margin-left: -100px;   "></div>  
                <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; margin-top:30px;   margin-left: -100px;   "></div>  
                <?php
            } else {
                if ($row['postion'] == '0' && $row['sub'] != 'strat') {
                    ?>  

                    <span style=" position: fixed; font-size: 13px; top:-7px; margin-left: 8px;"><?php echo $row['points']; ?></span>
                    <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/tri.jpg" alt="" style="float: left;" />
                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; padding-top: 15px; margin-left: -15px;   "></div> 

                    <?php
                } elseif ($row['postion'] == '90') {
                    $mtop = '-120px';
                    ?>
                   <span style=" position: fixed; font-size: 13px;  margin-left:-140px; margin-top:-90px;"><?php echo $row['points']; ?></span>
                    <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/tri.jpg" alt="" style="float: left; margin-left:-140px; margin-top:-90px;" />
                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:50px; transform: rotate(90deg); margin-left:-155px;  margin-top:-35px; ; ">fdg</div> 

                    <?php
                    $q = mssql_query("SELECT *  FROM  plot_points WHERE  sub ='" . $row['points'] . "'");
                    $sstop = 90;
                    $t = 1;
                    $top1 = 0;
                    while ($row1 = mssql_fetch_array($q)) {
                        // echo $row1['postion'];
                        if ($row1['postion'] == '90') {

                            $mttop = $sstop + 90;
                            $ttp = '-' . $mttop . 'px';
                            $ttp5 = '-' . $mttop + 50 . 'px';
                            $left = 140;
                            $le = '-' . $left . 'px';
                            $ttptop = '-' . $mttop-10 . 'px';
                            $sstop = $mttop;
                            ?>
                            <span style=" position: fixed; font-size: 13px;margin-left:<?php echo $le; ?>; margin-top:<?php echo $ttptop; ?> ;"><?php echo $row1['next_points']; ?></span>

                            <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/<?php echo $row1['icons'] ?>" alt="" style="float: left; margin-left:<?php echo $le; ?>; margin-top:<?php echo $ttp; ?> ;" />
                            <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:50px; transform: rotate(90deg); margin-left:-155px;  margin-top:<?php echo $ttp5; ?> ; "></div> 


                        <?php } elseif ($row1['postion'] == '180') { ?>
                            <?php
                            $q1 = mssql_query("SELECT *  FROM  plot_points WHERE  points ='" . $row1['points'] . "'");

                            while ($row2 = mssql_fetch_array($q1)) {
                                $mttop1 = $top1 + 90;
                                $ttp1 = '-' . $mttop1 . 'px';
                                $ttp2 = '-' . $mttop1 - 10 . 'px';
                                $ttp3 = '-' . $mttop1 - 20 . 'px';
                                $ttpb = '-' . $mttop1-10 . 'px';
                                if ($row2['postion'] == '180') {
                                    ?>
                              <span style=" position: fixed; font-size: 13px;margin-left:-290px; margin-top:<?php echo $ttpb; ?> ;"><?php echo $row2['next_points']; ?></span>

                                    <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/<?php echo $row2['icons'] ?>" alt="" style="float: left; margin-left:-270px; margin-top:<?php echo $ttp1; ?> ;" />
                                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; transform: rotate(180deg); margin-left:-230px;  margin-top:<?php echo $ttp1; ?> ; "></div> 
                                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; transform: rotate(180deg); margin-left:-230px;  margin-top:<?php echo $ttp2; ?> ; "></div> 
                                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:100px; transform: rotate(180deg); margin-left:-230px;  margin-top:<?php echo $ttp3; ?> ; "></div> 
                                <?php } else { ?>

                                <?php }
                                ?>


                            <?php }$top1 = 90;
                            ?>
                        <?php } ?>

                        <?php
                        $top += 90;

                        $t++;
                    }
                    ?>
                    <?php
                } elseif ($row['postion'] == '270') {
                    $mtop = '90px';
                    ?>
                    <img id="<?php echo $row['points']; ?><?php echo $row['pid']; ?>" src="img/<?php echo $row['icons'] ?>" alt="" style="float: left; margin-left:-140px;margin-top:<?php echo $mtop; ?> " />
                    <div class="progressbar<?php echo $i + 1; ?>"  style=" float: left;width:57px; transform: rotate(270deg); margin-left:-125px;  margin-top:45px; ; "></div> 
                    <span style=" position: fixed; font-size: 13px;margin-left:-140px;margin-top:130px; "><?php echo $row['next_points']; ?></span>
                        <?php
                } elseif ($row['postion'] == '180') {
                    //echo 'vbn';
                }
                ?>

            <?php }
            ?>

            <?php
            $i++;
        }
        echo ' </div> ';
        ?>

    </div>
import { Component, OnInit } from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-crud',
  templateUrl: './crud.component.html',
})
export class CrudComponent implements OnInit {
  myvehicle: FormGroup;
  display: boolean;
  id: number;
  vin: any;
  year: number;
  brand: string;
  color: string;
  vehicle: any;
  Data: any;

  constructor(private activatedRoute: ActivatedRoute, private fb: FormBuilder) {
   }

  ngOnInit() {
    this.myvehicle = this.fb.group({
    vin: [''],
    year: [''],
    brand: [''],
    color: ['']
  });
    this.vehicle = [
      {
       id: 1 , vin: 'dsad231ff' , year : 2012 , brand: 'VW' , color: 'Orange'
      },
      {
        id: 2 , vin: 'gwregre345' , year : 2011 , brand: 'Audi' , color: 'Black'
      },
      {
        id: 3 , vin: 'h354htr' , year : 2005 , brand: 'Renault' , color: 'Gray'
      },
      {
        id: 4, vin: 'j6w54qgh' , year : 2003 , brand: 'BMW', color: 'Blue'
      },
      {
        id: 5, vin: 'hrtwy34' , year : 1995 , brand: 'Mercedes' , color: 'Orange'
      }
    ];
    debugger
    this.activatedRoute.paramMap
    .subscribe( params => {
    this.id = +params.get('id');
    });

      this.vehicle.forEach(element => {
        if (element.id === this.id) {
            this.Data = element;
              }
      });
      this.myvehicle.patchValue({
        vin: this.Data.vin,
        year: this.Data.year,
        brand: this.Data.brand,
        color:  this.Data.color
      });
  }

}

1 个答案:

答案 0 :(得分:1)

我认为主要问题在这里

<input type="text" [formControlName]="vin" ><br><br>

您尝试传递一个空变量

vin: any;

作为控件名称,可以通过以下方式固定:

<input type="text" formControlName="vin" ><br><br>

只需移除方括号并将控件名称设置为字符串即可。

它应该解决您的问题。

另外,这部分

  this.activatedRoute.paramMap
.subscribe( params => {
this.id = +params.get('id');
});

  this.vehicle.forEach(element => {
    if (element.id === this.id) {
        this.Data = element;
          }
  });
  this.myvehicle.patchValue({
    vin: this.Data.vin,
    year: this.Data.year,
    brand: this.Data.brand,
    color:  this.Data.color
  });

可以简化

    this.activatedRoute.paramMap
  .subscribe(params => {
    const id = params.get('id');

    if (this.vehicle[id]) {
      this.myvehicle.patchValue(this.vehicle[id]);
    }
  });

仅当您在路由器参数中具有正确的ID时,patchValue才会运行