gpt4 book ai didi

javascript - 仅当对象类型为数组时如何使用 ng-repeat?

转载 作者:行者123 更新时间:2023-11-30 07:21:23 24 4
gpt4 key购买 nike

我有一个复杂的对象,如下所示:

$scope.document = 
{
"GENERAL_FIELDS": {
"Source_Type": "custom",
"Annotations": [
"216/content/Factiva_CM_001/Proteins",
"216/content/Factiva_CM_001/Fact"
],
"Content": [
" Baculovirus; Budded virus; Ultrastructure; Cryo-EM;"
],
"Title": [
"Budded baculovirus particle structure revisited"
]
},
"stn": {
"Document_Type": [
"Journal",
"Article"
]
}
}

我想显示“GENERAL_FIELDS”和“stn”中存在的所有字段。字段的值可以是字符串或字符串数​​组。如果它是数组,我还想对其进行 ng-repeat 并显示内容。以下是我的 html:

<div id="titsec" class="comdocdet"  ng-repeat="(category, group) in document">
<div ng-repeat="(key, value) in group">
<div class="pTitle">
{{key}}
</div>
<div class="contdesc">
<div ng-if="Array.isArray(value)">
<div ng-repeat="v in value">
{{v}}
</div>
</div>
<div ng-if="!Array.isArray(value)">
{{value}}
</div>
</div>
</div>
</div>

ng-if="Array.isArray(value)" 永远不会为真,数组字段以对象形式显示:["Journal","Article"]。我错过了什么?

最佳答案

或者将其添加到您的 Controller 中并保持原样。

 $scope.isArray = angular.isArray;

html 应该是这样的:

<div ng-if="isArray(value)">
<div ng-repeat="v in value">
{{v}}
</div>
</div>
<div ng-if="!isArray(value)">
{{value}}
</div>

关于javascript - 仅当对象类型为数组时如何使用 ng-repeat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40772095/

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