[英]How to use ng-repeat only if object type is array?
I have a complex object as shown below: 我有一个复杂的对象,如下所示:
$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"
]
}
}
I want to display all the fields present in "GENERAL_FIELDS" and "stn". 我想显示“ GENERAL_FIELDS”和“ stn”中存在的所有字段。 Fields' value can either be string or array of strings.
字段的值可以是字符串或字符串数组。 If it is array, I further want to ng-repeat on it and display the content.
如果是数组,我还想对其进行ng-repeat并显示内容。 Following is my html:
以下是我的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>
But ng-if="Array.isArray(value)"
is never true and array fields are being displayed in object form: ["Journal","Article"]
. 但是
ng-if="Array.isArray(value)"
永远不会正确,并且数组字段将以对象形式显示: ["Journal","Article"]
。 What am I missing ? 我想念什么?
Instead of accessing a method on the Array object directly in the template, you should do in your controller. 您应该在控制器中执行操作,而不是直接在模板中访问Array对象上的方法。 So for example:
因此,例如:
<div ng-if="vm.isValueAnArray(value)">
// Some html
</div>
Your controller: 您的控制器:
function isValueAnArray(val) {
return Array.isArray(val);
}
I haven't tested it, but logic should be in the controller, not in the template. 我还没有测试过,但是逻辑应该在控制器中,而不是模板中。
Or add this in your controller and leave rest like it is. 或将其添加到您的控制器中,并保持原样。
$scope.isArray = angular.isArray;
html would be like this : html将是这样的:
<div ng-if="isArray(value)">
<div ng-repeat="v in value">
{{v}}
</div>
</div>
<div ng-if="!isArray(value)">
{{value}}
</div>
The scope of the template is relative to $scope
in the controller, so when it looks for Array , it will look for that in the controller scope (eg $scope.Array
). 模板的范围是相对于控制器中的
$scope
的,因此,当它查找Array时 ,它将在控制器范围中进行查找(例如$scope.Array
)。
One option is to use ng-if="window.Array.isArray(value)"
. 一种选择是使用
ng-if="window.Array.isArray(value)"
。 See the working example below. 请参阅下面的工作示例。
Another option is to set $scope.Array = Array.prototype
in the controller. 另一个选择是在控制器中设置
$scope.Array = Array.prototype
。 That way there is no need to reference window
before calling Array.isArray()
. 这样,在调用
Array.isArray()
之前无需引用window
。
Another option is to create an alias for Array.isArray() in the controller scope: 另一个选择是在控制器范围内为Array.isArray()创建别名:
$scope.isValueAnArray = Array.isArray;
Then call that function to determine if the value is an array. 然后调用该函数以确定该值是否为数组。
angular.module('ang', []) .controller('cont', function($scope) { //use this to avoid referencing window in the template //$scope.Array = Array.prototype; $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" ] } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="ang" ng-controller="cont"> <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="window.Array.isArray(value)"> <div ng-repeat="v in value"> {{v}} </div> </div> <div ng-if="!window.Array.isArray(value)"> {{value}} </div> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.