6

I have a component MyComp and I would like to pass a function to it as parameter. More precisely I would like to do something like that:

dart component file:

 @NgComponent(
        selector: 'mycomp',
        publishAs: 'ctrl',
        map: const {
          'myfunc' :'=> myfunc'
        }
    )
class MyComponent {
   Function myfunc;

   ....
   myfunc();
}

html:

<mycomp myfunc="ctrl.myfunc"></button-list>

The problem is that myfunc is null in the component. Do I miss something? How can I do that?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
odwl
  • 2,095
  • 2
  • 17
  • 15

2 Answers2

7

Use '&' to bind a function to a field:

@NgComponent(
    selector: 'mycomp',
    publishAs: 'ctrl',
    map: const {
      'myfunc' :'&myfunc'
    }
)
class MyComponent {
    Function myfunc;

   ....
   myfunc();
}

http://ci.angularjs.org/view/Dart/job/angular.dart-master/javadoc/angular.core/NgComponent.html#map

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Ozan
  • 4,345
  • 2
  • 23
  • 35
  • 1
    Great. To pass parameter, it there something simpler than myfunc({'\$myparam': xyz} with the html being ? – odwl Dec 07 '13 at 20:58
  • 1
    Hmm, do you want to pass a parameter to mycomp when binding the function or from mycomp when calling myfunc? – Ozan Dec 07 '13 at 23:07
  • I want to pass a parameter when calling myfunc. – odwl Dec 08 '13 at 10:12
  • Just call it like any other function. When binding it, in the html just write . Hint: '$' in identifiers is reserved for angular-specific members. – Ozan Dec 08 '13 at 14:27
  • 4
    To pass parameters you'll need to pass a `locals` scope when calling the function: `myfunc({'param1': paramVal1, 'param2': paramVal2});` and when binding `` – pavelgj Dec 08 '13 at 22:34
  • When calling the function you need to use – alearg Jan 04 '14 at 14:51
  • ...and parameters need to be maps – alearg Jan 04 '14 at 18:56
6

The preferred way in AngularDart is to use annotations

@NgCallback('myfunc') Function myFunc;
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567