首页 > 解决方案 > 如何在外部方法之外获取返回值

问题描述

我是 Angular 的新手,我对获取它的返回值的问题感到困惑。我已经在另一个内部实现了一个功能。

private validateKeyterm(): boolean {
        const val = this.form.value.term;      
        if (this.selectedTermType == 'keywords') {
            // filter out data
            this.keywordTemp.filter(function (d) {
              if((d.term.toString().indexOf(val) !== -1 || !val) == true){
                    console.log("keytemp: true");
                    return true;
                }
             
            }) ;
        } else {
            this.profanityTemp.filter(function (d) {
                if((d.term.toString().indexOf(val) !== -1 || !val) == true){
                    console.log("profanity: true");
                    console.log("true");
                    return true;
                }
                else{ return null;}

            });
        }
        console.log("false");
        return false;
    }
 

但是在此函数执行中,我总是将返回值设为“失败”。谁能找到我的解决方案,从“validateKeyterm”函数中获取“真实”案例。

标签: arraysangulartypescripttype-conversiondata-conversion

解决方案


我必须检查您在此函数中使用的值,但根据您的代码,我建议您在过滤器函数中使用箭头函数,如下所示:

  private validateKeyterm(): boolean {
    const val = this.form.value.term;      
    if (this.selectedTermType == 'keywords') {
        // filter out data
        return !!(this.keywordTemp.filter(d => {
          if((d.term.toString().indexOf(val) !== -1 || !val) == true){
                console.log("keytemp: true");
                return true;
            }
         
        }));
    } else {
        return (this.profanityTemp.filter( d => {
            if((d.term.toString().indexOf(val) !== -1 || !val) == true){
                console.log("profanity: true");
                console.log("true");
                return true;
            }
            else{ return null;}

        }));
    }
    console.log("false");
    return false;
}

推荐阅读