【发布时间】:2018-02-05 06:21:00
【问题描述】:
这个函数应该将某些数字推送到我要在视图中显示的数组中。
@Component({
selector: 'app-task2',
templateUrl: './task2.component.html',
styleUrls: ['./task2.component.css']
})
export class Task2Component implements OnInit {
array: [number];
input;
constructor() {}
ngOnInit() {}
}
一切正常,但只要我包含“this.array.push(i)”,它就会在提交时不断刷新我的页面(在提交时调用此函数)。我删除它,一切正常,它以正确的方式迭代,找到数字和它应该做的一切。即使我在函数内部创建数组并推送到它,它也可以正常工作,但是当我将它用作类“属性”并使用“this”作为参考时它不起作用(开始刷新)。
onSubmit() {
for(var i=2; i<=this.input; i++) {
if(this.input % i == 0) {
this.input /= i;
this.array.push(i);
return this.onSubmit(); //does same even without this
}
}
this.input = "";
}
【问题讨论】:
-
请分享您的模板代码。顺便说一句,你为什么需要在内部调用
onSubmit?
标签: javascript arrays angular arraylist push