html - 角 | 在其他按钮上运行事件
问题描述
(无效问题,不需要参考,谢谢。)
解决方案:使用公共变量而不是 $event。
我想在其他按钮上运行该事件。
我使用$event
,但我不能立即调用函数。
.html
<p-fileUpload (onSelect)="runSelect($event)"
accept="image/*" #imageUpload></p-fileUpload>
<button (click)="callImageSelect()" #button2></button>
当 button2 被按下时,我想在#imageUpload 上运行 (onSelect) 事件。
(onSelect) 有一个参数。(event.files:选定文件列表)
如果按 button1 运行,
我想把默认值放在 $event 中。
所以我发现的方法是使用viewChild
.
.ts
@ViewChild('imageUpload') imageUpload;
...
callImageSelect() {
this.imageUpload.**onSelect()**; // In this section, call (onSelect).
}
runSelect(event) {
event.files.push(defalut);
// Here I put the default value for event.
}
有什么好办法吗?
感谢您的意见。
解决方案
你想要的是触发事件onSelect
。这里要注意的一点onSelect
不是 a function
but event
。您不应该调用实际触发它的event
调用。function
由于您使用onSelect
的是自定义事件,因此您必须使用一些Directive
相同的事件。所以你应该做的是,访问组件内部的指令并调用触发事件的函数(EventEmitter);
Sue-do 代码
@Directive(selector="my-directive")
public class MyDirective {
value : string;
onSelect = new EventEmitter();
public selectionMade(){
this.onSelect.emit(value);
}
}
组件 html
<button (onSelect)="runSelect($event)" myDirective #myDirective="myDirective"></button>
<button (click)="clickTest()" #button2></button>
组件 ts
@ViewChild('myDirective') myDirective : MyDirective;
...
clickTest() {
this.myDirective.selectionMade(); // In this section, call (onSelect).
}
推荐阅读
- ssl - 尝试访问我网站的 XML 文件时出现 cURL 60 错误
- vue.js - 如果 VueJS 模型属性不在原始属性列表中,则它们不会绑定
- javascript - 将 json 键映射到 javascript 对象
- c - C中堆上的数组分配
- javascript - 如何从所有这些元素创建 CSV 文件?
- python - 使用标量操作 Dataframe 列值
- flowtype - 'U' 缺少类型注释
- nginx - Lua 中的 collectgarbage 是否还包括 lua_shared_dict 消耗的内存?
- java - @Autowired 和 @SpringBootTest 应该在单元测试中使用吗?
- javascript - 将道具传递给本地状态初始值是否是一种不好的做法以及如何正确执行