Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
获取值后订阅的Angular/Typescript句柄结果_Angular_Typescript - Fatal编程技术网

获取值后订阅的Angular/Typescript句柄结果

获取值后订阅的Angular/Typescript句柄结果,angular,typescript,Angular,Typescript,我希望这个问题没有答案,我找不到任何对我的案例有帮助的东西。我对angular和typescript有点陌生,所以:调用rest调用时,如何提取订阅返回的值? 代码如下所示: static utilAccountName(....., queryService: QueryService) { ... let value = true; let name; do { .... /

我希望这个问题没有答案,我找不到任何对我的案例有帮助的东西。我对angular和typescript有点陌生,所以:调用rest调用时,如何提取订阅返回的值? 代码如下所示:

    static utilAccountName(....., queryService: QueryService) {
        ...
        let value = true;
        let name;
        do {
            ....
           // the name is built and should be changed here until there shouldn't be in the backend
            value = this.theValueExists(name: string, queryService: QueryService);
        } while (value);
        return name;
    }

    private static theValueExists(name: string, queryService: QueryService): boolean {
        let val;
        queryService.findValueByName(name)
            .subscribe(
            (res) => {
                val= res!= null;
            }, () => {
                val= false;
            }
        );
        return val;

    }

   //the function from the queryService looks like this
    findValueByName(name: string): Observable<MyObject> {
        return this.httpClient
            .get<MyObject>('/rest/byName/' + name)
            .map(result => {
                if (<any>result) {
                    return new MyObject().deserialize(result);
                }
            })
    }
static utilAccountName(…,queryService:queryService){
...
让值=真;
让名字;
做{
....
//该名称已生成,并应在此处更改,直到后端中没有该名称为止
value=this.theValueExists(名称:string,queryService:queryService);
}而(价值);
返回名称;
}
private static theValueExists(名称:string,queryService:queryService):布尔值{
让瓦尔;
queryService.findValueByName(名称)
.订阅(
(res)=>{
val=res!=null;
}, () => {
val=假;
}
);
返回val;
}
//queryService中的函数如下所示
findValueByName(名称:字符串):可观察{
返回此.httpClient
.get('/rest/byName/'+name)
.map(结果=>{
如果(结果){
返回新的MyObject()。反序列化(结果);
}
})
}

我遇到的问题是,来自
theValueExists
val
返回一个
undefined
,我需要它在对后端的调用完成并返回结果后返回值
true
false
。有办法做到这一点吗?

findValueByName
是一种异步操作。使用int
值退出
时,您可以
返回值在执行子划分块之前

queryService.findValueByName(name)
            .subscribe(
            (res) => {
                val= res!= null;
            }, () => {
                val= false;
            }
        );
最好的方法是在其他方法的基础上定义进一步的观测值<代码>值存在
将变为

private theValueExists(name: string, queryService: QueryService): Observable<boolean> {
  return queryService.findValueByName(name).pipe(
    map(res => res != null),
    catchError(err => of(false)),
  );
private theValueExists(名称:string,queryService:queryService):可观察{
返回queryService.findValueByName(名称).pipe(
映射(res=>res!=null),
catchError(err=>of(false)),
);

而且
utilAccountName
还需要用相应的rxjs util来处理这些可观察对象。

你能给我解释一下你用
管道做什么吗
?我从来没有见过这样的事情,而且你有
似乎很奇怪,之后什么都没有。它仍然需要
可观察的
。这是你answ的一部分er missing?
pipe
是一种可观察的方法,需要链接rxjs操作符来转换数据流。请联系rxjs基础知识。