Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Typescript 移除(i)角度5格式阵列的问题_Typescript_Angular5 - Fatal编程技术网

Typescript 移除(i)角度5格式阵列的问题

Typescript 移除(i)角度5格式阵列的问题,typescript,angular5,Typescript,Angular5,在我运行这个方法之后 const control = <FormArray>this.proposalForm.get('proposals'); control.removeAt(i); 当我尝试运行ComputeBalance时,ValueChanges返回null,因为控件现在为null 请问,当我运行AddMorePropositions()动态添加更多表单时,如何取消对已删除控件的注册,使其再次可用 感谢以下是如何声明和销毁订阅: x: any; mySub: Subsc

在我运行这个方法之后

const control = <FormArray>this.proposalForm.get('proposals');
control.removeAt(i);
当我尝试运行ComputeBalance时,ValueChanges返回null,因为控件现在为null

请问,当我运行AddMorePropositions()动态添加更多表单时,如何取消对已删除控件的注册,使其再次可用


感谢

以下是如何声明和销毁订阅:

x: any;
mySub: Subscription;

ngOnInit() {
  this.mySub.subscribe(val => this.x = val); 
}

ngOnDestroy() {
  this.mySub.unsubscribe();
}
根据您的描述,removeAt(i)发生在computeBalances()之前。这会导致未定义的错误

要防止出现这种情况,请尝试在subscribe调用中调用removeAt:

this.proposalForm.get('proposals.' + this.count + '.ProposedAmount').valueChanges.subscribe(value => {
 if (this.count == 0) {
     this.details.CurrentBalance = this.originalBalance - value;
 } else {
     this.details.CurrentBalance = this.latestOutstandingBalance - value
 }
 //do the removeAt(i) here.
})

谢谢,我想这已经解决了。我把t

his.myFormValueChanges$ = this.proposalForm.controls['proposals'].valueChanges;
        this.myFormValueChanges$.subscribe(units => this.updateCalculations(units));
在我的ngOnInt()中

然后我实现了如下更新计算

private updateCalculations(units: any) {
        const control = <FormArray>this.proposalForm.controls['proposals'];
        // console.log(units)
        for (let i in units) {
            if (units[i].ProposedAmount != '') {
                if (i == "0") {
                    this.details.CurrentBalance = this.originalBalance - units[i].ProposedAmount;
                }
                else {
                    this.details.CurrentBalance = this.latestOutstandingBalance - units[i].ProposedAmount;
                }
                this.latestOutstandingBalance = this.details.CurrentBalance;
            }



            const endDate = new Date(units[i].EndDate.year, units[i].EndDate.month - 1, units[i].EndDate.day);
            const startDate = new Date(units[i].StartDate.year, units[i].StartDate.month - 1, units[i].StartDate.day);
            let dateDiff = +endDate.getMonth() - +startDate.getMonth();
            let monthDifference = dateDiff + (12 * (endDate.getFullYear() - startDate.getFullYear()));
            let yearDifference = monthDifference / 12;

            if (units[i].PaymentPlan == 1) {
                control.at(+i).get('InstallmentAmount').setValue((units[i].ProposedAmount / (monthDifference / units[i].Frequency)).toFixed(2), { onlySelf: true, emitEvent: false })
            }

            else if (units[i].PaymentPlan == 2) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (monthDifference / (3 * units[i].Frequency))).toFixed(2), { onlySelf: true, emitEvent: false })

            }
            else if (units[i].PaymentPlan == 3) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (yearDifference / (2 * units[i].Frequency))).toFixed(2), { onlySelf: true, emitEvent: false })
            }

            else if (units[i].PaymentPlan == 4) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (yearDifference / units[i].Frequency)).toFixed(2), { onlySelf: true, emitEvent: false })

            }

            else {
                control.at(+i).get('InstallmentAmount').setValue((units[i].ProposedAmount / units[i].Frequency).toFixed(2), { onlySelf: true, emitEvent: false })

            }

        }
私有更新计算(单位:任意){
const control=this.proposalForm.controls['proposals'];
//控制台日志(单位)
for(以单位表示){
if(单位[i]。建议装载量!=''){
如果(i==“0”){
this.details.CurrentBalance=this.originalBalance-units[i]。建议安装量;
}
否则{
this.details.CurrentBalance=this.latestOutstandingBalance-单位[i]。建议安装量;
}
this.latestOutstandingBalance=this.details.CurrentBalance;
}
const endDate=新日期(单位[i].endDate.year,单位[i].endDate.month-1,单位[i].endDate.day);
const startDate=新日期(单位[i]。startDate.year,单位[i]。startDate.month-1,单位[i]。startDate.day);
让dateDiff=+endDate.getMonth()-+startDate.getMonth();
让monthDifference=dateDiff+(12*(endDate.getFullYear()-startDate.getFullYear());
设年差=月差/12;
if(单位[i]。付款计划==1){
控件.at(+i).get('InstallmentAmount').setValue((单位[i].ProposedAmount/(月差/units[i].Frequency)).toFixed(2),{仅限您自己:true,emitEvent:false})
}
else if(单位[i]。付款计划==2){
control.at(+i).get('InstallmentAmount').setValue(((单位[i].ProposedAmount)/(月差/(3*单位[i].Frequency))).toFixed(2),{onlySelf:true,emitEvent:false})
}
else if(单位[i]。付款计划==3){
control.at(+i).get('InstallmentAmount').setValue(((单位[i].ProposedAmount)/(年差/(2*单位[i].Frequency)).toFixed(2),{仅自身:真,发射事件:假})
}
else if(单位[i]。付款计划==4){
control.at(+i).get('InstallmentAmount').setValue(((单位[i].ProposedAmount)/(年差/单位[i].Frequency)).toFixed(2),{onlySelf:true,emitEvent:false})
}
否则{
control.at(+i).get('InstallmentAmount').setValue((单位[i].ProposedAmount/units[i].Frequency).toFixed(2),{onlySelf:true,emitEvent:false})
}
}

现在一切正常。感谢您的帮助

请查看我上面的回复。谢谢。我明白了。看起来.subscribe(value=>…中的值为空。您可以执行类似以下操作来取消订阅:
this.logsSub.UnSubscribe()
但最好知道何时删除该控件。可能是在ComputeBalance()之前先删除它。如果您还有其他问题和其他信息,请告诉我。您好,请问我应该取消订阅的this.logsAb是什么?我在运行AddMorePropositions()时运行ComputeBalance()添加另一个动态表单。如果我没有运行removeAt(i),一切都会好的。但一旦我运行removeAt(i),所有后续的AddMorePropositions()将返回未定义的控件。我会假设您的proposalForm是订阅,因为您使用了.subscribe with它。
this.proposalForm.get('Propositions.+this.count+'.ProposedAmount').valueChanges.subscribe
根据您的描述,removeAt(i)首先出现,这就是它导致未定义错误的原因。为了防止这种情况,请在.subscribe.this中执行removeProposalField(i:number)函数removeProposalFields(i:number):void{const control=this.proposalForm.get('proposals');let removedAmount=this.proposalForm.get('proposals').value[i].ProposedAmount;this.latestOutstandingBalance=this.details.CurrentBalance+removedAmount;control.removeAt(i);}你的意思是我应该把它放在一个亚脚本里吗??
private updateCalculations(units: any) {
        const control = <FormArray>this.proposalForm.controls['proposals'];
        // console.log(units)
        for (let i in units) {
            if (units[i].ProposedAmount != '') {
                if (i == "0") {
                    this.details.CurrentBalance = this.originalBalance - units[i].ProposedAmount;
                }
                else {
                    this.details.CurrentBalance = this.latestOutstandingBalance - units[i].ProposedAmount;
                }
                this.latestOutstandingBalance = this.details.CurrentBalance;
            }



            const endDate = new Date(units[i].EndDate.year, units[i].EndDate.month - 1, units[i].EndDate.day);
            const startDate = new Date(units[i].StartDate.year, units[i].StartDate.month - 1, units[i].StartDate.day);
            let dateDiff = +endDate.getMonth() - +startDate.getMonth();
            let monthDifference = dateDiff + (12 * (endDate.getFullYear() - startDate.getFullYear()));
            let yearDifference = monthDifference / 12;

            if (units[i].PaymentPlan == 1) {
                control.at(+i).get('InstallmentAmount').setValue((units[i].ProposedAmount / (monthDifference / units[i].Frequency)).toFixed(2), { onlySelf: true, emitEvent: false })
            }

            else if (units[i].PaymentPlan == 2) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (monthDifference / (3 * units[i].Frequency))).toFixed(2), { onlySelf: true, emitEvent: false })

            }
            else if (units[i].PaymentPlan == 3) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (yearDifference / (2 * units[i].Frequency))).toFixed(2), { onlySelf: true, emitEvent: false })
            }

            else if (units[i].PaymentPlan == 4) {
                control.at(+i).get('InstallmentAmount').setValue(((units[i].ProposedAmount) / (yearDifference / units[i].Frequency)).toFixed(2), { onlySelf: true, emitEvent: false })

            }

            else {
                control.at(+i).get('InstallmentAmount').setValue((units[i].ProposedAmount / units[i].Frequency).toFixed(2), { onlySelf: true, emitEvent: false })

            }

        }