Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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
Angularjs 这里有没有办法限制if条件的需要?_Angularjs_Typescript_Nativescript - Fatal编程技术网

Angularjs 这里有没有办法限制if条件的需要?

Angularjs 这里有没有办法限制if条件的需要?,angularjs,typescript,nativescript,Angularjs,Typescript,Nativescript,作为后续工作,我已经成功地对我的可观察对象应用了多个过滤器,这取决于用户决定应用的过滤器 我遇到的“问题”是,不确定是否应用了过滤器,甚至所有过滤器的工作方式都不同;有些是数字,有些是字符串 我的观察结果如下: getProperties(): Observable<Property[]> { return this.http.get<Property[]>(this.url); } getProperties():可观察{ 返回this.http.get(th

作为后续工作,我已经成功地对我的可观察对象应用了多个过滤器,这取决于用户决定应用的过滤器

我遇到的“问题”是,不确定是否应用了过滤器,甚至所有过滤器的工作方式都不同;有些是数字,有些是字符串

我的观察结果如下:

getProperties(): Observable<Property[]> {
    return this.http.get<Property[]>(this.url);
}
getProperties():可观察{
返回this.http.get(this.url);
}
并应用我的动态过滤器,如下所示:

filterAndSort() {
    let count = 0;
    return this.getProperties()
    //Only return properties where price is higher than or equal to min price
    .map(properties => properties.filter((property) => {
        if(property.price >= this.userSettings.getAppSetting("filterMinPrice", "number")) {
            return property;
        }
    })
    .filter((property) => {
        //Return all properties if max price is not set (0)
        if(this.userSettings.getAppSetting("filterMaxPrice", "number") == 0) {
            return property;
        }
        //Only return properties where price is lower than or equal to max price
        else if(property.price <= this.userSettings.getAppSetting("filterMaxPrice", "number")) {
            return property;
        }
    })
    .filter((property) => {
        if(property.incomeCategory == this.userSettings.getAppSetting("filterIncomeClass", "string")) {
            return property;
        } else if (this.userSettings.getAppSetting("filterIncomeClass", "string") == "none") {
            return property;
        }
    })
    .filter((property) => {
        if(property.numberOfRooms >= this.userSettings.getAppSetting("filterMinRooms", "number")) {
            return property;
        }
    })
    .filter((property) => {
        if(property.numberOfBedrooms >= this.userSettings.getAppSetting("filterMinBedrooms", "number")) {
            return property;
        }
    })
    .sort((a: Property, b: Property) => {
        //..
        }
    ))
}
filterAndSort(){
让计数=0;
返回此参数。getProperties()
//仅返回价格高于或等于最低价格的房产
.map(属性=>properties.filter((属性)=>{
如果(property.price>=this.userSettings.getAppSetting(“filterMinPrice”、“number”)){
归还财产;
}
})
.filter((属性)=>{
//如果未设置最高价格(0),则返回所有属性
if(this.userSettings.getAppSetting(“FilterMaprice”,“number”)==0){
归还财产;
}
//仅返回价格低于或等于最高价格的房产
否则,如果(不动产价格){
if(property.incomeCategory==this.userSettings.getAppSetting(“filterIncomeClass”,“string”)){
归还财产;
}else if(this.userSettings.getAppSetting(“filterIncomeClass”,“string”)=“none”){
归还财产;
}
})
.filter((属性)=>{
如果(property.numberOfRooms>=this.userSettings.getAppSetting(“filterMinRooms”,“number”)){
归还财产;
}
})
.filter((属性)=>{
如果(property.numberofbeddrooms>=this.userSettings.getAppSetting(“filterMinBedrooms”,“number”)){
归还财产;
}
})
.sort((a:属性,b:属性)=>{
//..
}
))
}
它是有效的,但我认为这不是最好的解决方案,因为我一遍又一遍地调用
filter()
。我认为如果我设法减少过多的if条件,并且只在该条件下使用
return属性;
一次,我的代码将更加整洁。(请记住,我将在将来添加更多的过滤器选项,因此如果我必须以类似的方式应用它们,这肯定会变得更加混乱:p)

有什么建议吗


(为了澄清问题:我在这里使用NativeScript/Angular/Typescript)。

这可能会起作用。我不确定这是否是一种改进,但只是尝试将逻辑结果压缩为一个过滤函数:

.filter(property => {
      if(
        (
          this.userSettings.getAppSetting("filterMaxPrice", "number") == 0 ||
          property.price <= this.userSettings.getAppSetting("filterMaxPrice", "number")
        ) &&
        (
          property.incomeCategory == this.userSettings.getAppSetting("filterIncomeClass", "string") ||
          this.userSettings.getAppSetting("filterIncomeClass", "string") == "none"
        ) &&
        (
          property.numberOfRooms >= this.userSettings.getAppSetting("filterMinRooms", "number")
        ) &&
        (
          property.numberOfBedrooms >= this.userSettings.getAppSetting("filterMinBedrooms", "number")
        )
      ) {
          return property;
      }
  })
.filter(属性=>{
如果(
(
this.userSettings.getAppSetting(“FilterMaprice”,“number”)==0||
property.price=this.userSettings.getAppSetting(“filterMinRooms”、“number”)
) &&
(
property.numberofbeddrooms>=this.userSettings.getAppSetting(“filterMinBedrooms”,“number”)
)
) {
归还财产;
}
})

当然,这会删除很多不必要的代码。也许还有更好的方法,但如果不彻底重组我的整个应用程序,我想不出任何方法。谢谢你的帮助:)