Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters from directive to callback

I'm trying to define a directive sortable which wraps jqueryui's sortable plugin.

The angular code is:

module.directive('sortable', function () {
    return function (scope, element, attrs) {
        var startIndex, endIndex;
        $(element).sortable({
            start:function (event, ui) {
                startIndex = ui.item.index();
            },
            stop:function (event, ui) {
                endIndex = ui.item.index();
                if(attrs.onStop) {
                    scope.$apply(attrs.onStop, startIndex, endIndex);
                }
            }
        }).disableSelection();
    };
});

The html code is:

<div ng-controller="MyCtrl">
    <ol sortable onStop="updateOrders()">
         <li ng-repeat="m in messages">{{m}}</li>
    </ol>
</div>

The code of MyCtrl:

function MyCtrl($scope) {
    $scope.updateOrders = function(startIndex, endIndex) {
        console.log(startIndex + ", " + endIndex);
    }
}

I want to get the startIndex and endIndex in my callback updateOrders and do something with them, but it prints:

undefined, undefined

How to pass these parameters to my callbacks? Is my approach correct?

like image 709
Freewind Avatar asked Oct 09 '12 13:10

Freewind


3 Answers

This fiddle shows hot callback from a directive passing parameters. Main trick is to use the scope to pass a function. http://jsfiddle.net/pkriens/Mmunz/7/

var myApp = angular.module('myApp', []).
directive('callback', function() {
    return { 
        scope: { callback: '=' },
        restrict: 'A',
        link: function(scope, element) {
            element.bind('click', function() {
                scope.$apply(scope.callback('Hi from directive '));
            })
        }
    };
})

function MyCtrl($scope) {
    $scope.cb = function(msg) {alert(msg);};
}

Then the html looks like for example:

<button callback='cb'>Callback</button>
like image 166
Peter Kriens Avatar answered Nov 18 '22 00:11

Peter Kriens


scope.$apply accepts function or string. In this case using function would be simpler:

  scope.$apply(function(self) {
    self[attrs.onStop](startIndex, endIndex);
  });

Don't forget to change your html code to:

<ol sortable onStop="updateOrders">

(Removed the ())

like image 44
Tosh Avatar answered Nov 18 '22 01:11

Tosh


Alternative 1

If you don't have an isolate scope for this directive, I would use the $parse service for that:

In the controller:

...
$scope.getPage = function(page) {

   ...some code here...

}

In the view:

<div class="pagination" current="6" total="20" paginate-fn="getData(page)"></div>

In the directive:

if (attr.paginateFn) {
   paginateFn = $parse(attr.paginateFn);
   paginateFn(scope, {page: 5})
}

Alternative 2

Now, if you have an isolate scope, you can pass parameters to it as a named map. If your directive is defined like this:

scope: { paginateFn: '&' },

link: function (scope, el) {
   scope.paginateFn({page: 5});
}
like image 13
Edmar Miyake Avatar answered Nov 18 '22 01:11

Edmar Miyake