I started to learn angular2 some time ago and lately I've confronted with an issue that I can't completely understand to solve it properly.
Here is a component: I have cut out all excess code to look simpler
@Component({
moduleId: module.id.toString(),
selector: "editor-group",
providers: [],
template: `
<div class="section-box" >
<div *ngFor="let attr of attrList" >
<editor-block [structure]="opts" ></editor-block>
</div>
</div>
`
})
export class EditorGroup implements OnInit {
attrList: string[];
opts: any = CssEditorDPY[1];
constructor(private EditorService: EditorService, private _contextService: ContextService) {}
ngOnInit() {
this.attrList = ['option1'];
}
}
Here I have variable "opts" that I set already with some data to check that it is working, which I transferring to child component . So, this hard coded case working as expected and no magic happens, but when I'm trying to define structure for child component through the function, like below
@Component({
moduleId: module.id.toString(),
selector: "editor-group",
providers: [],
template: `
<div class="section-box" >
<div *ngFor="let attr of attrList" >
<editor-block [structure]="defineStructure(attr)" ></editor-block>
</div>
</div>
`
})
export class EditorGroup implements OnInit {
attrList: string[];
constructor(private EditorService: EditorService, private _contextService: ContextService) {}
ngOnInit() {
this.attrList = ['option1'];
}
defineStructure(option: string): EditorItem {
var item: EditorItem;
for (var i: number = 0; i < EditorDPY.length; i++) {
if (EditorDPY[i].name === option) { item = EditorDPY[i]; }
}
return item;
}
}
I noted that everything started working super slowly, then I found out that defineStructure()
function executes about 500 times in a second. Firstly I thought that probably this happens because of some rxjs observable, but eventually I cut off them all to be sure then commented almost all components except this but the problem still remains. At last I noted that defineStructure()
execute triggered by absolutely any mouse event: click, context, move ... when, for example, I moving a mouse over document, it executes ~200 for each 10 pixels.
By some reason this function executing from any mouse event when no data has been updated.
I've tried a lot, but I've run out of ideas, thinking need to understand the root of the reason. Please, advise.