Thorsten Rintelen
Thorsten Rintelen

Reputation: 568

AngularJS ES6 ui-grid directive scope, ng-click dont work

I have a chain directive with angularJS and ES6 and want to use ui-grid. The grid is shown with the correct columns and data, thats fine.

But ng-click don´t work in the cellTemplate. Nothing happens. Also i try to check the grid object with console.log(grid) and grid is undefined.

Who can i use the cellTemplate to call the openDetail method?

export default function ChainsDirective() {
    class ChainsDirective {

        /*@ngInject*/
        constructor(chainsService, $state) {
            this.chainsServiceLoadChains = chainsService.loadChains.bind(chainsService);
            this.gridOptions = {
                enableColumnMenus: false,
                columnDefs: [
                    {
                        name: 'id',
                        visible: false
                    },
                    {
                        name: 'name',
                        displayName: 'Kette',
                        cellTemplate: '<div class="ui-grid-cell-contents"><a onclick="console.log(grid)" ng-click="grid.appScope.openDetail(row.entity.id)">{{row.entity.name}}</a></div>'
                    }
                ]
            };
            this.$stateGo = $state.go.bind($state);
            this.fetch(); // best practice initiales laden in einer Funktion zu kapseln
        }

        /**
         * @param int chainId
         */
        openDetail(chainId) {
            this.$stateGo('chainDetail', {chainId})
        }

        fetch() {
            return this.chainsServiceLoadChains().then(data => {
                this.gridOptions.data = data;
            })
        }
    }

    return {
        restrict: 'E',
        template: '<div ui-grid="chains.gridOptions" external-scopes="$scope" class="grid"></div>',
        scope: {},
        bindToController: {},
        controller: ChainsDirective,
        controllerAs: 'chains'
    }
}

Upvotes: 2

Views: 760

Answers (1)

Stefan
Stefan

Reputation: 14880

I've never used ui-grid directive. But it seems that you need to access your controller by its name, since you have declared it with the "controller as" syntax.

So in your ng-click you need to reference the controller on the scope with its name chains:

ng-click="grid.appScope.chains.openDetail(row.entity.id)"

Upvotes: 3

Related Questions