gpt4 book ai didi

选择 ng-options : 中的对象的 Angularjs 过滤器不为空

转载 作者:行者123 更新时间:2023-12-01 11:24:49 26 4
gpt4 key购买 nike

我正在尝试在 Angular 1.5.6 中过滤掉属于对象而不是 null 的项目。

我尝试了这个 post 中的所有选项.

仍然无法正常工作。

select 中只应显示“Sally”

这是 fiddle

HTML :

<div ng-app="myApp" ng-controller="myCtrl">
<br>

<select ng-model="a" ng-options="detail.name for detail in details | filter:{shortDescription:'!null'} track by detail.name">
</select>

<br>
New

<select ng-model="b" ng-options="detail.name for detail in details | filter:{shortDescription: ''} track by detail.name">
</select>

<br>
All
<select ng-model="c" ng-options="detail.name for detail in details | filter:{shortDescription: '!!'} track by detail.name">
</select>

<br>
Without any filter
<select ng-model="d" ng-options="detail.name for detail in details track by detail.name">
</select>
<!-- -->
</div>

脚本:

function myCtrl($scope) {
$scope.details = [{
name: 'Bill',
shortDescription: null
}, {
name: 'Sally',
shortDescription: {'MyName':'A girl'}
}];
}

angular.module("myApp",[]).controller("myCtrl", myCtrl);

最佳答案

问题是 filter:{shortDescription: ''} 只匹配基元而不匹配复杂对象,而 shortDescription 在你的情况下是一个嵌套对象。
因此,请像这样使用 filter:{shortDescription:{$:''}}:

  <select ng-model="a" 
ng-options="detail.name for detail in details | filter:{shortDescription:{$:''}} track by detail.name">
</select>

关于选择 ng-options : 中的对象的 Angularjs 过滤器不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com