【问题标题】:In Dart Angular, how to pass functions to component在 Dart Angular 中,如何将函数传递给组件
【发布时间】:2013-12-07 15:50:31
【问题描述】:

我有一个组件 MyComp,我想将一个函数作为参数传递给它。 更准确地说,我想做这样的事情:

dart 组件文件:

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

   ....
   myfunc();
}

html:

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

问题是组件中的 myfunc 为 null。 我错过了什么吗?我该怎么做?

【问题讨论】:

    标签: dart angular-dart


    【解决方案1】:

    使用 '&' 将函数绑定到字段:

    @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

    【讨论】:

    • 太棒了。要传递参数,它比 myfunc({'\$myparam': xyz} 更简单,html 是 ?
    • 嗯,你是想在绑定函数时给mycomp传参数,还是在调用myfunc时从mycomp传参数?
    • 我想在调用myfunc的时候传一个参数。
    • 就像调用其他函数一样调用它。绑定它时,在 html 中只写 。提示:标识符中的“$”是为特定角度的成员保留的。
    • 要传递参数,您需要在调用函数时传递locals 作用域:myfunc({'param1': paramVal1, 'param2': paramVal2}); 和绑定&lt;mycomp myfunc='ctrl.myfunc(param1, param2)'&gt;
    【解决方案2】:

    AngularDart 中的首选方式是使用注解

    @NgCallback('myfunc') Function myFunc;
    

    【讨论】:

      猜你喜欢
      • 2016-08-28
      • 2017-10-10
      • 2019-09-10
      • 2020-11-07
      • 2021-03-08
      • 1970-01-01
      • 2017-10-11
      • 2020-10-17
      • 2018-10-03
      相关资源
      最近更新 更多