根据Angular Testing文档,要从测试中触发事件,我们triggerEventHandler()
在调试元素上使用该方法.这个方法需要event name
和object
.现在,如果我们使用the添加事件,这是有效的HostListener
.对于ex:@HostListener('mousemove', ['$event'])
或者添加一个document
关卡事件,我们这样做@HostListener('document:mousemove', ['$event'])
.
在我当前的指令实现中,因为我无法嵌套HostListeners,所以我document
使用document.addEventListener
内部a 添加级别事件HostListener
.
代码如下:
@HostListener('mousedown', ['$event'])
callMouseDown(event){
if (something) {
document.addEventListener('mousemove', this.callMouseMove.bind(this));
}
}
callMouseMove(event){
// do some computations.
}
Run Code Online (Sandbox Code Playgroud)
现在,我想触发从我的测试级别mousemove
添加的事件document
.当前的实现triggerEventHandler()
不起作用,即监听器未在测试中被触发.
我怎样才能让它发挥作用?任何人都可以帮我一些指导.
编辑:添加测试:
it('test spec', inject([MyService], (service: MyService) =>{
x = [];
//service calls the method
service.evtEmit.subscribe(e => {
x.push(e);
});
triggerEventHandler("mousedown", {pageX: 200, pageY: 300});
triggerEventHandler("document:mousemove", {pageX: 250, pageY: …
Run Code Online (Sandbox Code Playgroud) 我有一个带有查询字符串的URL,该字符串以特定字母开头和结尾.这是一个例子和我的方法:
我们假设地址栏中的网址是 "http://localhost:3001/build/?videoUrl=bitcoin.vid.com/money#/"
我首先使用window.location.href
它提取此URL 并将其保存在变量中x
.
现在,我想首先检查videoUrl
URL中是否存在,然后如果它可用,我拆分URL并提取所需的URL,即bitcoin.vid.com/money
let x = "http://localhost:3001/build/?videoUrl=bitcoin.vid.com/money#/";
let y;
let result;
if(x.indexOf("?videoUrl")>-1) {
y = x.split("?videoUrl=");
result = y[1].split("#")[0];
console.log("Resultant URL:", result);
}
Run Code Online (Sandbox Code Playgroud)
我觉得我写的整个代码有点麻烦.任何人都可以让我知道是否有更优雅的方式来做同样的事情?
注意:videoUrl
URL中始终不可用,因此请检查它是否存在.如果我需要进一步检查,还请告诉我?
谢谢.
安吉拉
我正在使用fs.copyFile
将文件从一个位置复制到另一个位置。我这样做两次是为了复制两个文件。这是多余的,我想通过一次调用将两个文件复制到目标来使我的代码更好?我怎样才能做到这一点?
fs.copyFile('src/blah.txt', 'build/blah.txt', (err) => {
if (err) throw err;
});
fs.copyFile('src/unk.txt', 'build/unk.txt', (err) => {
if (err) throw err;
});
Run Code Online (Sandbox Code Playgroud) 我有一个对象数组,每个对象有大约5个属性和相应的值.我想迭代数组中的每个对象,只从中选择两个属性并重建对象和数组.
例如:
let resArray = [
{"name": "Aaron", "id": 123, "sex": "male", "country": "usa"},
{"name": "Bert", "id": 456, "sex": "male", "country": "usa"},
{"name": "Brenda", "id": 657, "sex": "female", "country": "canada"},
{"name": "Chris", "id": 856, "sex": "male", "country": "usa"},
{"name": "Angela", "id": 113, "sex": "female", "country": "columbia"},
{"name": "Maria", "id": 569, "sex": "female", "country": "mexico"}];
Run Code Online (Sandbox Code Playgroud)
我想从对象中删除"country"和"sex"属性,并保留"name"和"id"属性并重建数组.即,输出应如下:
let outArray = [
{"name": "Aaron", "id": 123},
{"name": "Bert", "id": 456},
{"name": "Brenda", "id": 657},
{"name": "Chris", "id": 856},
{"name": "Angela", "id": 113},
{"name": …
Run Code Online (Sandbox Code Playgroud)