Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Javascript 如何处理';未找到任何结果';在搜索功能中_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 如何处理';未找到任何结果';在搜索功能中

Javascript 如何处理';未找到任何结果';在搜索功能中,javascript,angular,typescript,Javascript,Angular,Typescript,如果有人使用我的搜索功能搜索数据库中没有返回结果的内容,我会尝试显示“未找到结果”。我遇到的问题是,“找不到结果”会立即打印到屏幕上,然后在实际计算搜索查询时消失,如果找不到结果,则会重新出现。换言之,它的工作正常,只是它应该等到一个查询被实际触发和评估后,再将“未找到结果”打印到屏幕上。从概念上讲,最好的方法是什么?一方面,我突然想到我可以使用一个超时。但这并不能直接解决问题。那么,最好的方法是什么呢?这是我的函数代码: public get noResultsFound(): boolean

如果有人使用我的搜索功能搜索数据库中没有返回结果的内容,我会尝试显示“未找到结果”。我遇到的问题是,“找不到结果”会立即打印到屏幕上,然后在实际计算搜索查询时消失,如果找不到结果,则会重新出现。换言之,它的工作正常,只是它应该等到一个查询被实际触发和评估后,再将“未找到结果”打印到屏幕上。从概念上讲,最好的方法是什么?一方面,我突然想到我可以使用一个超时。但这并不能直接解决问题。那么,最好的方法是什么呢?这是我的函数代码:

public get noResultsFound(): boolean
{
    if (this.query && !this._isSearching && !this.hasResults) {
        return true;
    }
}
以下是我的视图代码:

<div *ngIf="inputHasFocus && noResultsFound" class="no-results-found">No Results Found</div>
未找到任何结果

承诺听起来像你需要的:D

类似的东西

search = (paramObj) : Promise<SomeDataClass> => {
    // Some search logic
}

requestSearch() {
    this.search(null).then((result) => {
        if (result.length === 0)
        {
            //logic for showing 0 results here
        }
        else{
            // Show result?
        }
    })
}
search=(paramObj):Promise=>{
//一些搜索逻辑
}
请求搜索(){
this.search(空)。然后((结果)=>{
如果(result.length==0)
{
//此处显示0结果的逻辑
}
否则{
//显示结果?
}
})
}
一个更完整的例子看起来有点像这样

class example {
    public someBool = false;

    public search = (paramObj): Promise<Array<string>> => {
        this.someBool = !this.someBool;
        return new Promise<Array<string>>((resolver) => {
            // This just simulates something taking time.
            setTimeout(() => {
                if (this.someBool) {
                    resolver([]);
                } else {
                    resolver(["hi","there"]);
                }
            }, 1000)
        });
    }

    public requestSearch() {
        this.search(null).then((result) => {
            if (result.length === 0) {
                console.log("empty")
            }
            else {
                console.log(result)
            }
        })
    }
}
类示例{
public someBool=false;
公共搜索=(paramObj):承诺=>{
this.someBool=!this.someBool;
返回新承诺((解析器)=>{
//这只是模拟一些需要时间的事情。
设置超时(()=>{
如果(这个,someBool){
解析器([]);
}否则{
分解器([“hi”,“there”]);
}
}, 1000)
});
}
公共请求搜索(){
this.search(空)。然后((结果)=>{
如果(result.length==0){
console.log(“空”)
}
否则{
console.log(结果)
}
})
}
}

承诺听起来像你需要的:D

类似的东西

search = (paramObj) : Promise<SomeDataClass> => {
    // Some search logic
}

requestSearch() {
    this.search(null).then((result) => {
        if (result.length === 0)
        {
            //logic for showing 0 results here
        }
        else{
            // Show result?
        }
    })
}
search=(paramObj):Promise=>{
//一些搜索逻辑
}
请求搜索(){
this.search(空)。然后((结果)=>{
如果(result.length==0)
{
//此处显示0结果的逻辑
}
否则{
//显示结果?
}
})
}
一个更完整的例子看起来有点像这样

class example {
    public someBool = false;

    public search = (paramObj): Promise<Array<string>> => {
        this.someBool = !this.someBool;
        return new Promise<Array<string>>((resolver) => {
            // This just simulates something taking time.
            setTimeout(() => {
                if (this.someBool) {
                    resolver([]);
                } else {
                    resolver(["hi","there"]);
                }
            }, 1000)
        });
    }

    public requestSearch() {
        this.search(null).then((result) => {
            if (result.length === 0) {
                console.log("empty")
            }
            else {
                console.log(result)
            }
        })
    }
}
类示例{
public someBool=false;
公共搜索=(paramObj):承诺=>{
this.someBool=!this.someBool;
返回新承诺((解析器)=>{
//这只是模拟一些需要时间的事情。
设置超时(()=>{
如果(这个,someBool){
解析器([]);
}否则{
分解器([“hi”,“there”]);
}
}, 1000)
});
}
公共请求搜索(){
this.search(空)。然后((结果)=>{
如果(result.length==0){
console.log(“空”)
}
否则{
console.log(结果)
}
})
}
}

因此,最终我能够解决这个问题,不是通过使用某种去盎司超时,而是通过改变函数关注的内容——可以说。通过这种方式处理,“未找到结果”不会在它应该触发之前触发。这就是我最终使用的:

public get noResultsFound(): boolean
{
    if (!Object.isNullOrUndefined(this._results) && this._results.length < 1) {
        return true;
    } 
}
public get noResultsFound():布尔值
{
如果(!Object.isNullOrUndefined(this.\u results)&&this.\u results.length<1){
返回true;
} 
}

因此,最终我能够解决这个问题,不是通过使用某种去盎司超时,而是通过改变函数关注的内容——可以说。通过这种方式处理,“未找到结果”不会在它应该触发之前触发。这就是我最终使用的:

public get noResultsFound(): boolean
{
    if (!Object.isNullOrUndefined(this._results) && this._results.length < 1) {
        return true;
    } 
}
public get noResultsFound():布尔值
{
如果(!Object.isNullOrUndefined(this.\u results)&&this.\u results.length<1){
返回true;
} 
}