Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 基于方法可用性创建RxJs observable_Typescript_Rxjs_Observable - Fatal编程技术网

Typescript 基于方法可用性创建RxJs observable

Typescript 基于方法可用性创建RxJs observable,typescript,rxjs,observable,Typescript,Rxjs,Observable,在Angular 4应用程序中使用RxJs observable和TypeScript执行之前,我想等待方法可用性 到目前为止,我提出了使用interval与skipWhile相结合的方法,但我想知道是否有更好的方法来“观察”可用的方法,而不是每100毫秒观察一次 可观察 .间隔(100) .skipWhile(()=>myObject.myFunction==null) .first() .subscribe(()=>myObject.myFunction()); 我是一个新的观察者,所以任

在Angular 4应用程序中使用RxJs observable和TypeScript执行之前,我想等待方法可用性

到目前为止,我提出了使用
interval
skipWhile
相结合的方法,但我想知道是否有更好的方法来“观察”可用的方法,而不是每100毫秒观察一次

可观察
.间隔(100)
.skipWhile(()=>myObject.myFunction==null)
.first()
.subscribe(()=>myObject.myFunction());
我是一个新的观察者,所以任何洞察都是受欢迎的

//创建不带sayHello函数的元素
常量myElement={};
//在显示警报的1000毫秒后添加方法sayHello
setTimeout(()=>myElement.sayHello=()=>alert('Hello'),1000);
//如果sayHello函数在执行之前存在,则每100毫秒观察一次
可观测
.间隔(100)
.skipWhile((间隔)=>{
const isUndefined=myElement.sayHello==null;
写入(间隔,未定义);
返回未定义;
})
.first()
.subscribe(()=>myElement.sayHello());
//函数将结果写入DOM
常量写入=(间隔,未定义)=>{
const节点=document.createElement(“DIV”);
const textnode=document.createTextNode('interval#'+interval.toString()+'-sayHello是'+(isUndefined?'UNDEFINED':'DEFINED');
node.appendChild(textnode);
document.body.appendChild(节点);
};

如果你需要投票,你不能做得更好。正确的反应方式应该是
myObject
在方法准备就绪并且您订阅它时发出事件