Angular 2 @Output 参数

标签 angular

我试图通过@Output 传递参数,但被激发的函数只收到“未定义”。有人可以告诉我如何通过@Output 的 EventEmitter 传递参数吗?例如:

var childCmp = ng.core.Component({
             selector:'child-cmp',
             outputs: ['myEvent']
             }).Class({
               constructor: function(){
                            this.myEvent = new ng.core.EventEmitter();
                            this.myEvent.emit(false);
                            }
             });
var parentCmp = ng.core.Component({
              selector:'parent-cmp',
              template:'<child-cmp (myEvent)="invoke()"'></child-cmp>',
              directives: [childCmp]
           }).Class({
                constructor:function(){},
                invoke: function(flag){
                    // here flag is undefined!!
                }
             });

最佳答案

您应该使用以下方法获取事件提供的值:

<child-cmp (myEvent)="invoke($event)"'></child-cmp>'

这样,您的 childCmpinvoke 方法将接收您在发出 myEvent 自定义事件时提供的值作为参数。

希望对你有帮助, 蒂埃里

关于Angular 2 @Output 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34917932/

相关文章:

angular - ng2-auto-complete ngModel 不改变值

angular - 如何在 Angular 解析器的管道中进行过滤?

Angular 5 : Karma Unit testing fails to start HeadlessChrome on CI environment

javascript - TS1086 : An accessor cannot be declared in an ambient context

Angular 2 : how to open ngx-popover programmatically

angular - 将 .json 文件导入 Angular - "resolves to a non-module entity and cannot be imported using this construct."

angular - 延迟加载 BrowserModule 已经加载

angular - 意外的 token 导出 - Angular 2

angular - 将部分 Observable 提取到一个新的 observable 中

javascript - 有没有办法合并文档值(购物车总价和数量)