andmattia
andmattia

Reputation: 385

bindToController Angular 1.6

I have an issue with my directive after migration to lastest angular.

.directive('documentGrid',
    function() {
        return{
            restrict: 'EA',
            scope: {
                documentData: '=',
                remove: '&',
                edit: '&',
                documentDatasources: '='
            },
            controller: 'DocumentCrtl',
            controllerAs: 'vmDocument',
            //bindToController: true,
            //transclude: true,
            templateUrl: '/Custom/Document/document.cshtml'
        };
    });

<div class="box" document-grid document-data="widget"
                             document-datasources="vm.datasource.data"
                             remove="vm.remove(item)"
                             edit="vm.openSettings(item,datasources)">
                        </div>

before move to lastest version it works fine with bind controller set to true but now it does't work. So I remove the bindToController : true and I need to use $scope to access to my varibile bind from html directive

I read guide, post and tutorial about directive but I don't find any solution.

Upvotes: 1

Views: 2642

Answers (1)

Groben
Groben

Reputation: 1386

Try to replace with this:

directive('documentGrid',
    function() {
        return{
            restrict: 'EA',
            bindToController: {
                documentData: '=',
                remove: '&',
                edit: '&',
                documentDatasources: '='
            },
            controller: 'DocumentCrtl',
            controllerAs: 'vmDocument',
            scope: {},
            //transclude: true,
            templateUrl: '/Custom/Document/document.cshtml'
        };
    });

Note that I changed bindToController and scope.

Then in your controller your must bind this, e.g.: var vm = this; at the first line of your controller preferably.

Finally you will be able to access your data in controller like this:

vm.documentData

See this fiddle: https://jsfiddle.net/2n5skwqj/794/ In the controller function I log the name.

Upvotes: 1

Related Questions