Test access controller in angular directive

62 views Asked by At

I need to test methods from a controller into a directive.

APP.angular.directive('miniCrud', function () {

    return {
        restrict: 'E',
        require:"ngModel",
        scope: {
            miniCrudHeader: '@',
            miniCrudConfig: '=',
            modelValues : "=ngModel"
        },
        templateUrl: 'templates/mini-crud.html',
        controller: ['$scope', 'lodash', 'filterFilter', 
            function($scope, _, filterFilter){

                $scope.vm = {
                    getValue: function(item, index) {
                        var prop = $scope.miniCrudConfig.fields[index].name;
                        return item[prop];
                    }

                // some other methods
                }
            }
        }]
     };
});

How can I acess $scope.vm.getValue()?

1

There are 1 answers

0
Chandermani On

I hope you know how to test a directive. Since your directive creates an isolated scope you have to use the isolatedScope function on the object returned by angular.element.

Once you create an html directive element in your test, compile it and link it to a scope. You can then use the compiled element to get hold of isolated scope

var s = angular.element(compiledAndLinkedDOM).isolatedScope();
s.vm.getValue();