文本框中未显示角度json值

麦可

我是Angular的新手,我不知道这里发生了什么。我有一个MVC控制器,可以为我提供正确的值,例如{PostalContactPerson:jeff},但是我的Angualar视图无法识别输入框中页面加载时的值。

请问如何将值输入文本框?我困惑为什么当从同一窗体组中显示其他值时它是“空白”的。

Form.component.ts

export class FormComponent implements OnInit {
  data: any = null;



 this.arService.get(id, true)
                .subscribe(data => {
                    this.loading = false;
                    this.ar = data;
                    this.attachments = this.ar.attachments.filter(e => e.type == Enums.AttachmentType.Normal);
                    **this.data = this.ensureData(data.formData);
                    this.ar.formData = this.data;**

  this.stationInformationForm = formBuilder.group({
    "businessNumbersGroup": formBuilder.group({
      "acn": [this.data.acn],
      "icn": [this.data.icn],
      "postalcontactperson": [this.data.postaladdress],
    }, ),
  });
}

ensureData(data: any): any {
        if (data == null) {
            data = {};
        }

 if (!data["postalcontactperson"]) {
        data["postalcontactperson"] = { state: "" };
    }

form.component.html

<div [formGroup]="stationInformationForm.controls['businessNumbersGroup']">
  <div class="row">
    <div class="col-sm-6">
      <div class="form-group">
        <label>ACN</label>
        <div class="form-description">Specify if applicable</div>
        <input type="text" [ngClass]="{'has-error': !stationInformationForm.controls['businessNumbersGroup'].valid && (stationInformationForm.controls['businessNumbersGroup'].controls['acn'].touched || workflowSections[0].submitted)}" formControlName="acn" [(ngModel)]="data.acn"
          class="form-control" />
        <hr/>
      </div>

      <div class="form-group">
        <label>postalcontactperson</label>
        <div class="form-description">Specify if applicable</div>
        <input type="text" [ngClass]="{'has-error': !stationInformationForm.controls['businessNumbersGroup'].valid && (stationInformationForm.controls['businessNumbersGroup'].controls['PostalContactPerson'].touched || workflowSections[0].submitted)}" formControlName="PostalContactPerson"
          [(ngModel)]="data.postalcontactperson" class="form-control" />
      </div>
      <hr/>
    </div>
  </div>
</div>

MVC模型

      public static AnnualReturnModel Create(AnnualReturn annualReturn)
            {
               return new AnnualReturnModel
                {
                    Id = annualReturn.Id,
                    FormData = annualReturn.FormData,
PostalContactPerson = annualReturn.FormData.PostalContactPerson,
    }
}

MVC表格资料

 public class AnnualReturnFormData
    {
        public int? ActiveSectionIndex { get; set; }

        #region Station Information
        public string LesseeNames { get; set; }

        //Postal Address
        public string PostalContactPerson { get; set; }
}

上面的json结果:

{"ActiveSectionIndex":0,"LesseeNames":"aaa","PostalContactPerson":"aa","PostalPosition":"aa","PostalEmail":"aa","PostalPhone":"aa","StationContactPerson":"aaa"}

调试说明在这里

在页面加载时,我的ACN会显示在文本框中,但不会显示PostalContactPerson。为什么?

当我在网站上发出警报时,联系人不会显示我的价值。警报(this.data.acn); //显示值为22222 alert(“ contact person =” + this.data.postalcontactperson); //显示未定义

有什么建议吗?我缺少什么,当我检查数据库时,JSON值正确显示。任何建议,不胜感激。ngModel我缺少什么?

香港专业教育学院将代码更新为以下:form.component.html

 <div [formGroup]="stationInformationForm.controls['postalAddressGroup']">
            <div class="row">
             <div class="col-sm-6">
                <div class="form-group">
                  <label>Contact Person</label>
                  <input type="text" formControlName="postalcontactperson"class="form-control" value={{annualReturn.postalcontactperson}} /> <!--value={{annualReturn.postalcontactperson}}-->

               </div>
               <div class="form-group">
                <label>Position</label>
                <input type="text" formControlName="postalposition" class="form-control" />
                <div class="validation-error" *ngIf="!stationInformationForm.controls['postalAddressGroup'].valid && (stationInformationForm.controls['postalAddressGroup'].get('postalposition').touched || workflowSections[0].submitted)">You must specify Position</div>
              </div>
            </div>

            <div class="row">
            <div class="col-sm-6">
              <div class="form-group">
               <label>Email</label>
               <input type="text" formControlName="postalemail" class="form-control" />
               <div class="validation-error" *ngIf="!stationInformationForm.controls['postalAddressGroup'].valid && (stationInformationForm.controls['postalAddressGroup'].get('postalemail').touched || workflowSections[0].submitted)">You must specify email</div>
             </div>
             <div class="form-group">
              <label>Phone</label>
              <input type="text" formControlName="postalphone" class="form-control" />
              <div class="validation-error" *ngIf="!stationInformationForm.controls['postalAddressGroup'].valid && (stationInformationForm.controls['postalAddressGroup'].get('postalphone').touched || workflowSections[0].submitted)">You must specify phone</div>
            </div>
          </div>
        </div>
          </div>
        </div> 

form.component.ts

 var id = this.route.snapshot.params['param'];
        if (id != null) {
            this.arService.get(param, true)
                .subscribe(data => {
                    this.loading = false;
                    this.annualReturn = data;
                    this.attachments = this.annualReturn.attachments.filter(e => e.type == Enums.AttachmentType.Normal);
                    this.data = this.ensureData(data.formData);
                    this.annualReturn.formData = this.data;

                    this.dateConfig.disableUntil = { year: this.annualReturn.year - 1, month: 6, day: 30 };
                    this.dateConfig.disableSince = { year: this.annualReturn.year, month: 7, day: 1 };
                    
                  //  this.testprop = this.data.postalcontactperson;
    
                    this.stationInformationForm = formBuilder.group({
                        "survey.region": [this.data.survey.region],
                        "lesseeNames": [this.data.stationName, Validators.required],
                       // "somethingfancy": [this.data.postalcontactperson],
                       "postalcontactperson": [this.data.postalcontactperson],

                         "postalAddressGroup": formBuilder.group({
                             "postalcontactperson": [this.data.postalcontactperson, Validators.required],
                             "postalposition": [this.data.postalposition, Validators.required],
                             "postalemail": [this.data.postalemail, Validators.required],
                             "postalphone": [this.data.postalphone, Validators.required],
                         }, ),
西瓦库玛·塔迪斯蒂(Sivakumar Tadisetti)

您指的formControlName是模板文件中的错误这是postalcontactperson不是PostalContactPerson 观察首都P...C...P...最好不要使用[(ngModel)]已使用过的Reactive Forms仅供参考,请参阅以下更改

工作堆闪电战

打字稿文件

export class AppComponent implements OnInit {
  addressForm: FormGroup;
  stationInformationForm: FormGroup;
  data: any = {
    acn: 1,
    icn: 2,
    postaladdress: {
      contactperson: "Michael",
      address: "Some Address"
    }
  };

  constructor(private formBuilder: FormBuilder) {}

  public ngOnInit() {
    this.stationInformationForm = this.formBuilder.group({
      businessNumbersGroup: this.formBuilder.group({
        acn: [this.data.acn, Validators.required],
        icn: [this.data.icn, Validators.required],
        postalcontactperson: [this.data.postaladdress.contactperson, Validators.required]
      })
    });

    // Getting inner form group
    this.addressForm = this.stationInformationForm.get(
      "businessNumbersGroup"
    ) as FormGroup;

    // Getting Form Changes instead of using `[(ngModel)]`
    this.addressForm.valueChanges.subscribe(data => console.log(data));
  }
}

模板文件

<div [formGroup]="addressForm">
    <label>ACN</label>
    <div class="form-description">Specify if applicable</div>
    <input type="text" [ngClass]="{'has-error': !addressForm.valid && addressForm.get('acn').touched }" formControlName="acn" class="form-control" />
    <hr />
    <label>postalcontactperson</label>
    <div class="form-description">Specify if applicable</div>
    <input type="text" [ngClass]="{'has-error': !addressForm.valid && addressForm.get('postalcontactperson').touched }" formControlName="postalcontactperson" class="form-control" />
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

文本未显示在文本框中

来自分类Dev

Json响应未在文本框中显示值

来自分类Dev

文本框未显示在GridView中

来自分类Dev

文本框未显示在GUI中

来自分类Dev

在选择中显示文本框的值

来自分类Dev

文本框未显示正在设置的值

来自分类Dev

角度材质:突出显示文本框中的单词

来自分类Dev

方法更改的字符串值未显示在数据绑定的文本框中

来自分类Dev

根据角度中的输入参数更改文本框值

来自分类Dev

根据角度中的输入参数更改文本框值

来自分类Dev

换行符未显示在文本框中

来自分类Dev

插入符未显示在文本框中

来自分类Dev

重定向的输出未显示在文本框中

来自分类Dev

文本框值在asp.net中未更新

来自分类Dev

更新后的值未存储在文本框中

来自分类Dev

在php中的文本框中显示iframe值

来自分类Dev

JSON解析以在文本框中检索和显示属性的值(WIndows Phone 8)

来自分类Dev

将2个文本框值相乘并显示在第3个文本框中

来自分类Dev

在文本框中快速显示Int值

来自分类Dev

PHP如何使结果的值显示在文本框中

来自分类Dev

在文本框中显示增值税值

来自分类Dev

文本框中显示的db的十进制值

来自分类Dev

报表查看器在文本框中显示值

来自分类Dev

无法在html文本框中显示0值

来自分类Dev

想要帮助文本框条件以显示php中的值

来自分类Dev

串联文本框值并在段落中显示

来自分类Dev

如何在DIV中多次显示文本框值

来自分类Dev

在视图中的文本框中显示数据集值

来自分类Dev

PHP如何使结果的值显示在文本框中