如何仅显示数组中的非空字符串值

时间:2017-09-04 13:22:12

标签: angularjs angularjs-ng-repeat angular-ng-if

我试图在我的视图中迭代JSON数组,但我只想显示具有非空字符串值的键。

design:
{name: "ux", value: "3", $$hashKey: "object:5198"}
{name: "graphic", value: "", $$hashKey: "object:5199"}
{name: "concept", value: "4", $$hashKey: "object:5200"}
{name: "photoshop", value: "", $$hashKey: "object:5201"}
{name: "illustrator", value: "5", $$hashKey: "object:5202"}
{name: "inDesign", value: ""}
{name: "afterEffects", value: ""}
{name: "premierePro", value: "1"}

如何在迭代数组时只获取数组的键,只显示具有实际值的数组。

我目前的代码是:

<div class="six columns">
                        <div ng-repeat="skill in employeeDetails.design | limitTo:5:0 | filter:{value:'! '}">
                            {{ skill.name | capitalize }} {{ skill.value }}
                        </div>
                    </div>

任何帮助都将受到高度赞赏!

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用ng-if

<div class="six columns">
     <div ng-repeat="skill in employeeDetails.design | limitTo:5:0" ng-if="skill.value !== '' ">
             {{ skill.name | capitalize }} {{ skill.value }}
      </div>
</div>

或更改您的filter

<div class="six columns">
         <div ng-repeat="skill in employeeDetails.design | limitTo:5:0 | filter:{value:'!! '}" ng-if="skill.value !== '' ">
                 {{ skill.name | capitalize }} {{ skill.value }}
          </div>
    </div>