Viv*_*ndi 52 eventemitter angular2-directives angular
我有一个指令来初始化DOM元素上的jQueryUI可排序.jQueryUI sortable还有一组触发某些操作的回调事件.例如,当您启动或停止排序元素时.
我想通过函数从这样的事件传递返回参数emit(),所以我实际上可以看到我的回调函数中发生了什么.我只是没有找到通过一个传递参数的方法EventEmiiter.
我目前有以下内容.
我的指示:
@Directive({
selector: '[sortable]'
})
export class Sortable {
@Output() stopSort = new EventEmitter();
constructor(el: ElementRef) {
console.log('directive');
var options = {
stop: (event, ui) => {
this.stopSort.emit(); // How to pass the params event and ui...?
}
};
$(el.nativeElement).sortable(options).disableSelection();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我Component使用指令发生的事件:
@Component({
selector: 'my-app',
directives: [Sortable],
providers: [],
template: `
<div>
<h2>Event from jQueryUI to Component demo</h2>
<ul id="sortable" sortable (stopSort)="stopSort(event, ui)">
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
</ul>
</div>
`
})
export class App {
constructor() {
}
stopSort(event, ui) { // How do I get the 'event' and 'ui' params here?
console.log('STOP SORT!', event);
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在函数中获得event和uiparams stopSort()?
以下是我到目前为止的演示:http://plnkr.co/edit/5ACcetgwWWgTsKs1kWrA?p = info
pix*_*its 104
EventEmitter支持一个参数,该参数作为$event事件处理程序传递.
将参数传递给事件对象时将其包装在emit:
this.stopSort.emit({ event:event, ui: ui });
Run Code Online (Sandbox Code Playgroud)
然后,当您处理事件时,使用$event:
stopSort($event) {
alert('event param from Component: ' +$event.event);
alert('ui param from Component: ' + $event.ui);
}
Run Code Online (Sandbox Code Playgroud)
Alf*_*avo 15
pixelbits答案在最终版本中发生了一些变化.如果您有多个参数,只需将其作为一个对象传递.
子组件:
this.stopSort.emit({event,ui});
@Output() stopSort= new EventEmitter<any>();
Run Code Online (Sandbox Code Playgroud)
父组件:
hereIsHeight(value) {
console.log("Height = " + value.event);
console.log("Title = " + value.ui);
}
Run Code Online (Sandbox Code Playgroud)
父组件中的HTML:
<test-child1 (stopSort)="hereIsHeight($event)"></test-child1>
Run Code Online (Sandbox Code Playgroud)
- 此外,如果您有以下值:(前面带有"this")
this.stopSort.emit({this.event,this.ui});
Run Code Online (Sandbox Code Playgroud)
它们不起作用,你需要将它们改成别的东西,然后通过如下:
let val1 = this.event;
let val2 = this.ui;
this.stopSort.emit({val1,val2});
Run Code Online (Sandbox Code Playgroud)
*更新:阅读下面的Colin B的答案,了解使用"this"传递值的方法.
我无法添加评论,但只是想从Alpha Bravo的答案中指出您可以通过this.event,您不能使用属性值速记:
this.stopSort.emit({ event : this.event, ui : this.ui });
Run Code Online (Sandbox Code Playgroud)
还要注意,如果它们通过EventEmmiter传递,this.stopSort.emit({ val1, val2 });则它们将在父级中以以下方式访问:
hereIsHeight(value) {
console.log(`event = ${ value.val1 }`);
console.log(`ui = ${ value.val2 }`);
}
Run Code Online (Sandbox Code Playgroud)
因此,在这种情况下,最好避免使用速记来保持命名一致。
在孩子身上是这样工作的:
@Output() myEvent: EventEmitter<boolean> = new EventEmitter();
myFunc(value: boolean) {
this.myEvent.emit(value);
}
Run Code Online (Sandbox Code Playgroud)
现在您只需抓住父级中的事件即可!
| 归档时间: |
|
| 查看次数: |
64878 次 |
| 最近记录: |