我想将子组件的int值传递给其父组件。事件本身正确到达但我在父母的接收方法中添加Event参数失败。
更新:已缩减的已编辑代码,但完整示例
import 'package:angular2/core.dart';
class MyItem {
int id = 1;
}
@Component(
selector: 'my-list',
directives: const [MyListItem],
template: '''
<div *ngFor='let myItem of myItems'>
<my-list-item [myItem]='myItem' (dirtiesParent)='doInit($event)'></my-list-item>
</div>
''')
class MyList {
@Input()
List<MyItem> myItems = [ new MyItem() ];
void doInit() {
print("got event");
}
}
@Component(
selector: 'my-list-item',
template: '''
<div>id {{myItem.id}}<button (click)='doDelete()'>delete</button></div>
''')
class MyListItem {
@Input()
MyItem myItem;
@Output()
EventEmitter<int> dirtiesParent = new EventEmitter();
void doDelete() {
dirtiesParent.add(myItem.id);
}
}
答案 0 :(得分:2)
我认为你错误地逃过了$
。试试这个:
class MyItem {
int id = 1;
}
@Component(
selector: 'my-list',
directives: const [MyListItem],
template: '''
<div *ngFor='let myItem of myItems'>
<my-list-item [myItem]='myItem' (dirtiesParent)='doInit(\$event)'></my-list-item>
</div>
''')
class MyList {
@Input()
List<MyItem> myItems = [new MyItem()];
void doInit(int id) {
print("got event for id $id");
}
}
@Component(
selector: 'my-list-item',
template: '''
<div>id {{myItem.id}}<button (click)='doDelete()'>delete</button></div>
''')
class MyListItem {
@Input()
MyItem myItem;
@Output()
EventEmitter<int> dirtiesParent = new EventEmitter();
void doDelete() {
dirtiesParent.add(myItem.id);
}
}
这适合我(它打印&#34;得到id 1和#34的事件;)。请注意,我还将预期参数添加到doInit()
。