3

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • https://stackoverflow.com/a/43085186/6294072 :) – AT82 Jul 28 '17 at 12:25
  • Thank you very much, you have saved my mind from explosion. And I'm sorry, it was pretty hard to compose the question strictly and I couldn't find the answer. – Alan Menken Jul 28 '17 at 20:12
  • Glad to hear that I saved your head :D No need to apologize, I for example have made the exact same "mistake" so not to worry. Have a good weekend and happy coding! :) – AT82 Jul 28 '17 at 20:29
  • Possible duplicate of [angular2 - infinite loop when i call method from a Angular 2 class inside template](https://stackoverflow.com/questions/43081810/angular2-infinite-loop-when-i-call-method-from-a-angular-2-class-inside-templa) – Brian Tompsett - 汤莱恩 Oct 15 '19 at 19:44

0 Answers0