You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在函数中根据条件为priceFilter赋值布尔值?代码实现求助

修正价格过滤器赋值逻辑

Hey there! Let's get your price filter logic working correctly. The key mistake in your current code is that you're comparing the entire $scope.options array to individual string values—this will never match, since an array can't be equal to a single string. Instead, you need to use the priceFilter parameter that gets passed into your function (that's the selected option value from your list!).

修正后的完整代码

$scope.options = ['Low Price To High', 'High Price To Low']; 
$scope.selectPriceFilter = function (priceFilter) { 
  // 用传入的priceFilter参数做判断,而不是整个options数组
  if (priceFilter === 'Low Price To High') { 
    $scope.priceFilter = false; 
  } else if (priceFilter === 'High Price To Low') { 
    $scope.priceFilter = true; 
  } 
}

更简洁的写法(可选)

如果想让代码更紧凑,你可以直接利用布尔表达式的结果赋值,因为你的判断逻辑刚好对应布尔值需求:

$scope.options = ['Low Price To High', 'High Price To Low']; 
$scope.selectPriceFilter = function (priceFilter) { 
  $scope.priceFilter = priceFilter === 'High Price To Low';
}

这里当选中"High Price To Low"时,表达式返回true;选中另一个选项时返回false,完美匹配你的需求。

内容的提问来源于stack exchange,提问作者Bhawneet Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:27:28