如何为请求模型分配对象值

时间:2019-09-04 09:37:51

标签: angular angular-material angular7

我使用mat选项卡。在这种情况下,如果vailid则获取值。否则返回null。并将最终对象值分配给请求对象..

 saveSettings() {

            if (this.msmsForm.valid) {
                const smsSetting: MasterSMSSetting = {
                    SMSEnabled: this.msmsForm.get('allowOverRide').value,
                    AllowUserOverride: this.msmsForm.get('smsEnabled').value,
                    SMSURL: this.msmsForm.get('smsUrl').value,
                    SMSLogUrl: this.msmsForm.get('smsLogUrl').value,
                    ClientId: this.msmsForm.get('clientId').value,
                    AccesToken: this.msmsForm.get('accessToken').value
                };
            }
            if (this.memailForm.valid) {
            const emailsetting: MasterEmailSetting = {
                SEmailEnabled: this.memailForm.get('allowOverRide').value,
                AllowOverride: this.memailForm.get('emailEnabled').value,
                EmailUrl: this.memailForm.get('emailUrl').value,
                EmailLogUrl: this.memailForm.get('emailLogUrl').value,
                ClientId: this.memailForm.get('clientId').value,
                AccesToken: this.memailForm.get('accessToken').value
            };
        }
            const request: MasterSettingRequest = {
                masterSMS: smsSetting, // getting error "Cannot find name smsSetting'"
                masterEMail: emailsetting, //getting error "Cannot find name smsSetting"
                RequestType: this.requestType
            };


this.mastersettingservice.saveMasterSetting(request).subscribe((response: Response)

在这里,我写了if valid.im由于关闭if块而无法获取那些对象值!请让我们知道如何将这些值分配给请求对象

2 个答案:

答案 0 :(得分:1)

尝试使用与requestType相同的全局类变量:

emailsetting: MasterEmailSetting;
smsSetting: MasterSMSSetting;

saveSettings() {
  if (this.msmsForm.valid) {
    this.smsSetting = {
      SMSEnabled: this.msmsForm.get('allowOverRide').value,
      AllowUserOverride: this.msmsForm.get('smsEnabled').value,
      SMSURL: this.msmsForm.get('smsUrl').value,
      SMSLogUrl: this.msmsForm.get('smsLogUrl').value,
      ClientId: this.msmsForm.get('clientId').value,
      AccesToken: this.msmsForm.get('accessToken').value
    };
  }
  if (this.memailForm.valid) {
    this.emailsetting = {
      SEmailEnabled: this.memailForm.get('allowOverRide').value,
      AllowOverride: this.memailForm.get('emailEnabled').value,
      EmailUrl: this.memailForm.get('emailUrl').value,
      EmailLogUrl: this.memailForm.get('emailLogUrl').value,
      ClientId: this.memailForm.get('clientId').value,
      AccesToken: this.memailForm.get('accessToken').value
    };
  }
  const request: MasterSettingRequest = {
    masterSMS: this.smsSetting,
    masterEMail: this.emailsetting,
    RequestType: this.requestType
  };
  this.mastersettingservice.saveMasterSetting(request).subscribe((response: Response)

答案 1 :(得分:0)

您可以从以下代码获取解决方案:

saveSettings() {

        if (this.msmsForm.valid) {
            const smsSetting  = {
                SMSEnabled: this.msmsForm.get('allowOverRide').value,
                AllowUserOverride: this.msmsForm.get('smsEnabled').value,
                SMSURL: this.msmsForm.get('smsUrl').value,
                SMSLogUrl: this.msmsForm.get('smsLogUrl').value,
                ClientId: this.msmsForm.get('clientId').value,
                AccesToken: this.msmsForm.get('accessToken').value
            };
        }
        if (this.memailForm.valid) {
            const emailsetting = {
                SEmailEnabled: this.memailForm.get('allowOverRide').value,
                AllowOverride: this.memailForm.get('emailEnabled').value,
                EmailUrl: this.memailForm.get('emailUrl').value,
                EmailLogUrl: this.memailForm.get('emailLogUrl').value,
                ClientId: this.memailForm.get('clientId').value,
                AccesToken: this.memailForm.get('accessToken').value
            };
        }
        const request = {
                masterSMS: smsSetting, // getting error "Cannot find name smsSetting'"
                masterEMail: emailsetting, //getting error "Cannot find name smsSetting"
                RequestType: this.requestType
            };


            this.mastersettingservice.saveMasterSetting(request).subscribe((response: Response)