Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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
Javascript 根据与对象属性匹配的下拉选项筛选列表_Javascript_Angularjs - Fatal编程技术网

Javascript 根据与对象属性匹配的下拉选项筛选列表

Javascript 根据与对象属性匹配的下拉选项筛选列表,javascript,angularjs,Javascript,Angularjs,在我的页面上有一个使用ng repeat的项目列表和一个使用ng选项选择元素的下拉菜单。当下拉选择与项目上的特定属性匹配时,我只希望显示那些匹配的项目 列表项对象如下所示: var foo = { someProperty: 'someValue', someOtherProperty: 'someOtherValue', items: [{ propertyIWantToMatchOn: 'someImportantValue', /* Note tha

在我的页面上有一个使用ng repeat的项目列表和一个使用ng选项选择元素的下拉菜单。当下拉选择与项目上的特定属性匹配时,我只希望显示那些匹配的项目

列表项对象如下所示:

var foo = {
    someProperty: 'someValue',
    someOtherProperty: 'someOtherValue',
    items: [{
        propertyIWantToMatchOn: 'someImportantValue', /* Note that there will be a lot of items, but they all contain this property */
        someDisplayText: 'this can be arbitrary'
    }]
};
var myChoices = [
    {name: 'someImportantValue'},
    {name: 'someOtherImportantValue'}
];
<select ng-model="mySelectedChoice" ng-options="myChoice.name for myChoice in myChoices"></select> 
有时列表项的PropertyIwantMatch值为SomeImportantValue,有时列表项的值为someOtherImportantValue

我的选择列表如下所示:

var foo = {
    someProperty: 'someValue',
    someOtherProperty: 'someOtherValue',
    items: [{
        propertyIWantToMatchOn: 'someImportantValue', /* Note that there will be a lot of items, but they all contain this property */
        someDisplayText: 'this can be arbitrary'
    }]
};
var myChoices = [
    {name: 'someImportantValue'},
    {name: 'someOtherImportantValue'}
];
<select ng-model="mySelectedChoice" ng-options="myChoice.name for myChoice in myChoices"></select> 
我的选择下拉列表如下所示:

var foo = {
    someProperty: 'someValue',
    someOtherProperty: 'someOtherValue',
    items: [{
        propertyIWantToMatchOn: 'someImportantValue', /* Note that there will be a lot of items, but they all contain this property */
        someDisplayText: 'this can be arbitrary'
    }]
};
var myChoices = [
    {name: 'someImportantValue'},
    {name: 'someOtherImportantValue'}
];
<select ng-model="mySelectedChoice" ng-options="myChoice.name for myChoice in myChoices"></select> 
正在生成我的列表项,如下所示:

<ul>
    <li ng-repeat="item in foo.items | filter:{propertyIWantToMatchOn: mySelectedChoice}">{{item.someDisplayText}}</li>
</ul>
使用当前过滤器,无论选择什么选项,我的列表中都不会显示任何内容。如果删除过滤器,则无论选择什么,所有项目都会显示

如何修改过滤器/我的代码,以便只显示属性与下拉列表中的选择匹配的项目

干杯


编辑:如果我在控制器中将mySelectedChoice硬编码为与我试图匹配的属性值匹配的字符串,则列表将正确过滤。因此,我想现在我需要解决如何将选项保存为名称的值,而不是对象本身。

我设法解决了自己的问题。只需要一个小小的选择

在我的标记中,我只需要将筛选器:{propertyIWantToMatchOn:mySelectedChoice}更改为筛选器:{propertyIWantToMatchOn:mySelectedChoice.name}

谢谢大家

将ng选项与as一起使用,以便您可以更新ng-model中的名称。然后在ng repeat的过滤条件下使用list的ng模型

看到这个了吗


您想在ng repeat中显示什么?基于属性I想要匹配您想要列出的内容?项目是否只有PropertyWantMatch?请给出一个预期产出的例子。感谢您的关注。其实我想展示的东西是无关紧要的,我只想为属性与下拉选择匹配的所有项目显示项目显示文本:我已更新代码以包含我要显示的内容。我的解决方案解决了将名称另存为对象的问题。您可以使用ng选项在ngmodel中更新名称。是的,as选项正是我要查找的!您的代码示例完美地说明了这一点!