md-data-table icon indicating copy to clipboard operation
md-data-table copied to clipboard

Pass .bind($ctrl) to md-on-reorder causes invinite digest

Open scipper opened this issue 8 years ago • 2 comments
trafficstars

I have the the following reorder function in my controller class (ES2015):

class MyController {
    constructor() {
        this.newOrder = 'name';
    }

    onReorder(order) {
        this.newOrder = order;
    }
}

And I referenced the onReorder function like this:

<table md-table> <thead md-head md-order="$ctrl.newOrder" md-on-reorder="$ctrl.onReorder.bind($ctrl)">

Without .bind($ctrl) the context is obviously undefined. But passing .bind($ctrl) I receive the error:

vendor.js:56763 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

How can I properly pass the context of my controller to the onReorder function?

The answer from @daniel-nagy in changed how the onPaginate callback fn is bound seems kinda bad practice to me.

scipper avatar Sep 11 '17 08:09 scipper

Don't use bind in your template, use it in your constructor.

class MyController {
    constructor() {
        this.newOrder = 'name';
        this.onReorder = this.onReorder.bind(this);
    }

    onReorder(order) {
        this.newOrder = order;
    }
}

I think using bind is forbidden in angular expressions becuase it creates a new function,

You can't declare functions or create regular expressions from within AngularJS expressions.

Also binding the method in the constructor will be more efficient.

daniel-nagy avatar Sep 11 '17 14:09 daniel-nagy

This approach looks a bit better to me.

I did not know that .bind() returns a new function. Using thing technic on filters like

| filter: $ctrl.filterFunc.bind($ctrl)

works.

scipper avatar Sep 11 '17 15:09 scipper