Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.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
Javascript 单击复选框时,数组将丢失其值_Javascript_Angularjs - Fatal编程技术网

Javascript 单击复选框时,数组将丢失其值

Javascript 单击复选框时,数组将丢失其值,javascript,angularjs,Javascript,Angularjs,当我们点击复选框时,我调用了一个函数,在这个点击的基础上,我根据表单中选择的某一行调用url,从for循环中推送所有包含pdf的发票。我将控制台记录该数组,并且我能够找到可用的invoices数组,但是当我遇到一个条件,即如果数组中有内容,我们将显示一条消息时,该数组会变为空。有什么我不知道的吗。这是我的密码 我存在是真还是假。如果是真的,我们将该发票推入availableInvoicesPdf 如果为false,我们将该值推送到noAvailableInvoicesPdf中itemChecke

当我们点击复选框时,我调用了一个函数,在这个点击的基础上,我根据表单中选择的某一行调用url,从for循环中推送所有包含pdf的发票。我将控制台记录该数组,并且我能够找到可用的invoices数组,但是当我遇到一个条件,即如果数组中有内容,我们将显示一条消息时,该数组会变为空。有什么我不知道的吗。这是我的密码

我存在是真还是假。如果是真的,我们将该发票推入availableInvoicesPdf 如果为false,我们将该值推送到noAvailableInvoicesPdf中
itemChecked
是否与复选框绑定
ng model

onChange() {
    this.availableInvoicesPdf = [];
    this.noAvailableInvoicesPdf = [];
    this.allPdfResponseAvailable = false;
    for (let i = 0; i < this.invoices.length; i += 1) {
      // eslint-disable-next-line no-loop-func
      this.Invoice.hasPdf(this.invoices[i].id).then((isExists) => { // calls a function which have a function hasPdf and takes id of the invoices selected to return back the response
        if (isExists) {
          this.availableInvoicesPdf.push({ //pushing invoice id and number for the invoices which have pdf available. I made sure that it had a pdf everytime.
            id: this.invoices[i].id,
            number: this.invoices[i].number
          });
        } else {
          this.noAvailableInvoicesPdf.push({
            number: this.invoices[i].number,
          });
        }
// i dont have value for available invoices pdf here. Somethinf weird if i console log it then i am able to see it, but when i debug, the array is empty.
      });
    }
    this.showSuccessMessage = false;
    this.noInvoiceMessage = false;
    if (this.itemChecked && this.availableInvoicesPdf.length === 0 && this.noAvailableInvoicesPdf.length === 0) { // i dont have value for available invoices pdf here 
      this.checkInvoicesHasPdf();
      console.log('after running invoice pdf', this.availableInvoicesPdf);
      if (this.availableInvoicesPdf.length > 0 && this.noAvailableInvoicesPdf.length > 0) { // i dont have value for available invoices pdf here 
        this.showSuccessMessage = true;
        this.noInvoiceMessage = true;
      } else if (this.availableInvoicesPdf.length > 0 && this.noAvailableInvoicesPdf.length === 0) { // i dont have value for available invoices pdf here 
        this.showSuccessMessage = true;
      } else if (this.availableInvoicesPdf.length === 0 && this.noAvailableInvoicesPdf.length > 0) { // i dont have value for available invoices pdf here 
        this.noInvoiceMessage = true;
      }
    } else {
      this.attachDefaultInvoice = this.itemChecked;
    }
}
}

HTML:


{{'attachPdf'| translate}}

{{{'waitPdfUpload'| translate}{{{$ctrl.invoicesPdf.length}/ {{$ctrl.invoices.length}

发票PDF已成功上载


你能给我们看看
hasPdf
代码吗?当然可以。我更新了问题
this.Invoice.hasPdf
是异步的,您将其视为同步的。你点了比萨饼,在它送到你家之前你试着吃了它。谢谢你的回答。它真的帮助了我。我读了有关async/await的文章,现在我拿到了比萨饼,然后吃了起来。:)
hasPdf(id) {
const url = `${this.ARConstant.EXPORTING_REST_API_URL}pdf/${id}`;
return this.$http.head(url)
  .then(() => true)
  .catch(() => false);
<div class="pane-content" layout="column" ng-if="$ctrl.invoices.length <= 100">
    <div class="form-row" layout="row">
        <div class="attachPdf" flex="20">{{ 'attachPdf' | translate }}</div>
        <md-checkbox ng-disabled="$ctrl.isItaly" aria-label="{{ 'toAttach'| translate }}"
            class="attachPdf-cb md-primary" ng-model="$ctrl.itemChecked" ng-change="$ctrl.onChange()">
        </md-checkbox>
        <p ng-if="$ctrl.itemChecked && !$ctrl.allPdfResponseAvailable">{{ 'waitPdfUpload' | translate }} {{$ctrl.invoicesPdf.length}} /
            {{$ctrl.invoices.length}}</p>
    </div>
</div>

<div class="pane-content" layout="column" ng-if = "$ctrl.showSuccessMessage">
    <div class="form-row" layout="row">
        <div class="success-box-header-hidden" flex="20"></div>
        <div flex="80" class="success-message">
            <p class="success-text">
                <md-icon class="ionicons-svg-md-checkmark-circle" md-svg-src="img/ionicons-svg-md-checkmark-circle.svg"></md-icon>
                <span></span>Invoices PDF uploaded successfully</p>
        </div>
    </div>
</div>