简体   繁体   中英

Call function for ng-repeat from directive with parameters

I'm trying to create a reusable list-like directive. Example code is without styling, etc.

The code does invoke the callback but no argument is passed. Ideally, it would return the object for that row or its index.

  • The passed function 'callback' comes from a controller.
  • The directive has no controller directly associated to it.

Any help would be greatly appreciated.

Directive:

module.directive('quicklist', function() {
    return {
        restrict:'E',
        scope: {
            data: '=ngModel',
            clickFn: '&callback'
        }
        template:'<ul><li ng-repeat="d in data"><a href="" ng-click="clickFn(d)">{{d}}</a></li></ul>'
    };
});

HTML:

<quicklist ng-model='array' callback='work(arg)'><quicklist>

When I pass functions to be used with parameters in the directive, i use the = binding:

scope: {
            data: '=ngModel',
            clickFn: '=callback'
        }

And in your HTML just pass the function's name without the argument:

<quicklist ng-model='array' callback='work'><quicklist>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM