Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度数据未定义_Angular - Fatal编程技术网

Angular 角度数据未定义

Angular 角度数据未定义,angular,Angular,我对某些代码有问题: 我想用从DB检索三个集合项来报价。(要加载的功能正常) 第一个可行,但下两个(需要第一个的数据)不起作用。它说未定义 代码如下: ngOnInit() { let id = this.route.snapshot.paramMap.get('id'); this.campaignService.getOneCampaign(id).then( (campaign: Campaign) => { this.campaign

我对某些代码有问题:

我想用从DB检索三个集合项来报价。(要加载的功能正常)

第一个可行,但下两个(需要第一个的数据)不起作用。它说未定义

代码如下:

ngOnInit() {
    let id = this.route.snapshot.paramMap.get('id');

    this.campaignService.getOneCampaign(id).then(
      (campaign: Campaign) => {
        this.campaign = campaign;
        if (campaign.camptype = "VIDEO") {
          this.unitcostvid = (Math.round(this.campaign.unitcost * this.campaign.durationvid * 10000)) / 10000;
          this.totalcost = this.unitcostvid * this.campaign.nbuser;
        }
        if (campaign.camptype = "MAIL") {
          this.unitcostvid = this.campaign.unitcost;
          this.totalcost = this.unitcostvid * this.campaign.nbuser;
        }
        if (campaign.status = "EA") {
          this.eacamp = true;
        }

        return this.campaign;
      }
    )
    if (this.campaign) {
      this.usercService.getSingleUserc(this.campaign.usercidvalidate).then(
        (userc: Userc) => {
          this.userc = userc;
          return this.userc;
        }
      )
    }
    if (this.campaign) {
      this.companyService.getCompanybycoid(this.campaign.coid).then(
        (company: Company) => {
          this.company = company;

          return this.company;
        }
      )
    }
  }
我想要
活动
公司
用户c
值,但我只得到
活动


请帮助我。

首先请将campaign.camptype=“VIDEO”更改为campaign.camptype=“VIDEO” campaign.camptype=“MAIL”到campaign.camptype=“MAIL” campaign.status=“EA”到campaign.status==“EA”


然后再次运行您在
then
节中分配了活动,但尝试将它们用作执行
then
节之前发生的下一个语句。 像这样将其全部移动到内部:

ngOnInit() {
    let id = this.route.snapshot.paramMap.get('id');

    this.campaignService.getOneCampaign(id).then(
        (campaign: Campaign) => {
            this.campaign = campaign;
            if (campaign.camptype === "VIDEO") {
                this.unitcostvid = (Math.round(this.campaign.unitcost * this.campaign.durationvid * 10000)) / 10000;
                this.totalcost = this.unitcostvid * this.campaign.nbuser;
            }
            if (campaign.camptype === "MAIL") {
                this.unitcostvid = this.campaign.unitcost;
                this.totalcost = this.unitcostvid * this.campaign.nbuser;
            }
            if (campaign.status === "EA") {
                this.eacamp = true;
            }

            // no need to return this
            //return this.campaign;

            if (this.campaign) {
                this.usercService.getSingleUserc(this.campaign.usercidvalidate).then(
                    (userc: Userc) => {
                        this.userc = userc;
                        return this.userc;
                    }
                )
            }
            if (this.campaign) {
                this.companyService.getCompanybycoid(this.campaign.coid).then(
                    (company: Company) => {
                        this.company = company;

                        return this.company;
                    }
                )
            }
        }
    )
}
我把
=
改为
==
,因为我想你是想比较而不是分配…

有两个问题

首先,您正在设置一些
if
语句中的值,而不是检查是否相等

第二个原因是,
campaign
未定义的原因是使用它的代码在设置它的代码之前运行。当您使用承诺时,
then
中的代码将在数据加载到您的服务中后运行

要解决此问题,您可以在回调函数中设置用户和公司:

ngOnInit() {
    let id = this.route.snapshot.paramMap.get('id');

    this.campaignService.getOneCampaign(id).then(
      (campaign: Campaign) => {
        this.campaign = campaign;
        if (campaign.camptype == "VIDEO") {
          this.unitcostvid = (Math.round(this.campaign.unitcost * this.campaign.durationvid * 10000)) / 10000;
          this.totalcost = this.unitcostvid * this.campaign.nbuser;
        }
        if (campaign.camptype == "MAIL") {
          this.unitcostvid = this.campaign.unitcost;
          this.totalcost = this.unitcostvid * this.campaign.nbuser;
        }
        if (campaign.status == "EA") {
          this.eacamp = true;
        }

        if (this.campaign) {
            this.usercService.getSingleUserc(this.campaign.usercidvalidate).then(
              (userc: Userc) => {
                this.userc = userc;
              }
            )
          }

          if (this.campaign) {
            this.companyService.getCompanybycoid(this.campaign.coid).then(
              (company: Company) => {
                this.company = company;
              }
            )
          }
      }
    )
}

您的if语句不在承诺范围内,因此可以在本部分的“then”功能生效之前执行。