我是 Angular 的新手,所以这可能非常简单。 我有一些优惠的 JSON 数组。除其他外,每个报价都有一个价格。我希望能够添加一个过滤器,以便用户可以指定他们的最小/最大支出,并且只看到属于这些括号的优惠。
在列表 Controller 中我有
// MinMax filter:
$scope.minMax = function(prop, mn, mx){
return function(item){
if ( (Number(item[prop]) >= mn) && (Number(item[prop])<= mx) ) return true;
}
}
在“查看 HTML”中,我有两个简单的文本输入字段:
<input id='cheapest' name="cheapest" type='text' value='10' />
<input id='dearest' name="dearest" type='text' value='5000' />
和中继器
<div ng-repeat="offer in offers | filter: minMax('Price' , [?cheapest?], [?dearest?]) ">
当我硬编码值时,过滤器起作用,但如何将它们绑定(bind)到输入字段?
谢谢
最佳答案
这是对 Captain ngModel
的调用;)
只需绑定(bind)您的输入
值:
<input id='cheapest' name="cheapest" type='text' value='10' ng-model="cheapest" />
<input id='dearest' name="dearest" type='text' value='5000' ng-model="dearest" />
并将值传递给您的过滤器:
<div ng-repeat="offer in offers | filter: minMax('Price', cheapest, dearest) ">
或者通过您的 Controller 中的$scope.cheapest
访问它们。
虽然我不确定语法,但它会以某种方式工作:)
关于javascript - 是否可以将 HTML 输入字段变量传递给自定义 Angularjs 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28427228/