【问题标题】:Function inside ngIf is called multiple times, after a single click event- Angular2_TypescriptngIf 中的函数在单击事件后被多次调用 - Angular2_Typescript
【发布时间】:2016-12-20 11:14:01
【问题描述】:

您好,这是我第一次在 stackoverflow 上提出问题。 我正在 angular2: v2.1.0 (typescript- v2.0.10) 中开发一个应用程序。我正在使用“ng2-file-upload”进行文件上传。 HTML代码如下:

<div class="container-fluid">
    <h5 class="titleCenter"> File Upload </h5>
    <div class="drop-zone" ng2FileDrop
         [ngClass]="{'nv-file-over': hasBaseDropZoneOver, 'pointerBan': testDisablDropZone}"
         (fileOver)="fileOverBase($event)"
         [uploader]="uploader">
        <span *ngIf="isUploaderEmpty(uploader.queue)">Drop Down box for the CSV file only...!!!</span>
        <span *ngFor="let item of uploader.queue" >
            <p class="row" >
                <i class="fileIcon"></i>
                <strong>{{ item?.file?.name }}</strong>
                <a class="fileUploadRemoveButton fa fa-times-circle" (click)="item.remove()"></a>
            </p>
        </span>
    </div>
    <div  [ngClass]="{'activeCheckButton': testDisablDropZone}" class="CheckboxZone" (click)="disableDropZone($event)" style="margin-top: 10px">
        <span>Click here to disable the dropdown box.</span>
    </div>
</div>
<button type="button" (click)="test($event)">click</button>

在这里,当我单击带有“CheckboxZone”类的 div 时,它调用了函数“disableDropZone($event)”,但之后它也调用了函数“idUploadEmpty()”。与按钮单击相同的情况。 组件代码如下:

const URL = 'https://evening-anchorage-3159.herokuapp.com/api/';
@Component({
    selector: 'fileupload',
    templateUrl: './fileupload.component.html',
    styleUrls: ['./fileupload.component.scss']
})
export default class FileuploadComponent {
public uploader:FileUploader = new FileUploader({url: URL, autoUpload:true, allowedMimeType:['text/csv'] });
public hasBaseDropZoneOver:boolean = false;
public hasAnotherDropZoneOver:boolean = false;
private fileType =['json'];
private flagFile = false;
private testDisablDropZone = false;
private fileNotAccepted = false;
public fileOverBase(e:any):void {
    this.hasBaseDropZoneOver = e;
    console.log('EVENT fileOverBase : ', e);
}

public fileOverAnother(e:any):void {
    this.hasAnotherDropZoneOver = e;
    console.log('fileOverAnother : ', e);
}

isUploaderEmpty (uploaderQueue): boolean {
    console.log('Queue pqssed from View : ',this.uploader.queue);
    let qLength = uploaderQueue.length;
    if(uploaderQueue.length==0){
        this.fileNotAccepted = true;
        return true;}

    if (qLength > 1){
        uploaderQueue.pop();
        this.flagFile = true;
        let timer = Observable.timer(3000,10000);
        timer.subscribe(t=>{
            this.flagFile = false
        });
    }
    return false;
}

disableDropZone () {
    console.log('disableDropZone clicked...!!!');
    this.testDisablDropZone =! this.testDisablDropZone;
}

test(event)  {
    console.log('OK')
}
}

很难理解为什么它在触发事件时一直调用函数'isUploaderEmpty()'。

谢谢。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    其实很简单。

    当你定义一个 ngIf 时,你在里面放了一个表达式对吗?

    这意味着每次该组件内部有更新时,Angular 都需要确保评估 ngIf 中的表达式。 (这就是你对 Angular 的期望,对吧?否则你为什么要使用 ngIf?)

    因此,每次模型中有更新,或者更确切地说,每次触发 changeDetection 时,Angular 都会评估该表达式,在您的情况下,它是一个函数 (isUploaderEmpty)。

    通常,事件是触发 changeDetection 的事情之一(它比这更复杂)。

    这就是为什么 :D。

    【讨论】:

    • 你是对的。考虑到你的解释,我改变了我的代码。我将变量传递给 ngIf 而不是传递函数,它解决了问题。 :D
    • 是的。我刚试过。但我想知道 Ig 是否想在一个组件中使用 ngIf 两次,那我该怎么办?进行函数调用或其他什么方便吗?
    • @NRJ 是的,您实际上应该这样做,这是一种更好的做法,如果您将 .OnPush 用于您的 changeDetectionStrategy,那么调用 ngIf 的次数就会少得多
    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2020-11-27
    相关资源
    最近更新 更多