Yura Heineken
Yura Heineken

Reputation: 255

Execute function from directive

How to execute function from directive?

Or other variants to do this better.

Page:

<div ng-app="app" ng-controller="MainCtrl">
    <button ng-click=refresh()></button>
    <bs-table ???></bs-table>
</div>

Main controller:

angular.module('app', ['bsTable'])
    .controller('MainCtrl', ['$scope', function ($scope) {
        $scope.refresh = function() {
            ???
        };
}]);

Function added in directive's controller:

angular.module('bsTable', [])
    .directive('bsTable', [function () {
        return {
            restrict: 'EA',
            template: '<table></table>',
            replace: true,
            scope: {
                ???
            },
            link: function ($scope, $element) {
                $element.bootstrapTable();
            },                
            controller: function ($scope, $element) {
                $scope.refresh = function() {
                    $element.bootstrapTable('refresh');
                };
            }                
        }
    }]);

Upvotes: 1

Views: 242

Answers (1)

Harris
Harris

Reputation: 1785

In main controller:

$scope.functionToPass = function(anyParameter){ ... };

In directive:

...,
scope: {yourFunction:'=yourFunction'},
...,

In HTML:

<bs-table yourFunction="functionToPass">

Now you can access the function in the directive's controller and link as $scope.yourFunction.

Upvotes: 1

Related Questions