在包含对象的数组上显示ng-show

时间:2015-11-02 22:54:48

标签: angularjs

我在弄清楚如何在对象数组中使用具有特定属性的ng-if指令时遇到了一些麻烦。

阵列:

[{"Id":1087,
"CreateDate":"2015-11-02T19:26:39.007",
"Message":"Some text in here...",
"AffectedDate":null,
"Type":"a",
"CreatePeriod":"Today",
"CreateDateString":"Monday, November 2, 2015 7:26 PM"},
 {"Id":1086,
"CreateDate":"2015-11-02T19:26:25.2",
"Message":"Some text in here...",
"AffectedDate":null,
"Type":"a",
"CreatePeriod":"Yesterday",
"CreateDateString":"Monday, November 2, 2015 7:26 PM"},
 {"Id":1085,
"CreateDate":"2015-11-02T19:26:13.677",
"Message":"Some text in here...",
"AffectedDate":"2015-10-30T07:00:00",
"Type":"a",
"CreatePeriod":"Older",
"CreateDateString":"Monday, November 2, 2015 7:26 PM"}]

上面是一系列对象,我想在下一个标题上使用ng-show指令。

<h3 ng-show="">Last Week</h3>

我想与ng-show一起使用的属性是CreatePeriod

使用上述HTML,因为没有CreatePeriod具有&#34;上周&#34;我想隐藏那个标题。

这可以通过简单的ng-show实现吗?或者我是否需要将ng-repeat与ng-show结合使用?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在控制器中创建方法来检查您的情况:

$scope.data = [{
    "Id": 1087,
    "CreateDate": "2015-11-02T19:26:39.007",
    "Message": "Some text in here...",
    "AffectedDate": null,
    "Type": "a",
    "CreatePeriod": "Today",
    "CreateDateString": "Monday, November 2, 2015 7:26 PM"
}, {
    "Id": 1086,
    "CreateDate": "2015-11-02T19:26:25.2",
    "Message": "Some text in here...",
    "AffectedDate": null,
    "Type": "a",
    "CreatePeriod": "Yesterday",
    "CreateDateString": "Monday, November 2, 2015 7:26 PM"
}, {
    "Id": 1085,
    "CreateDate": "2015-11-02T19:26:13.677",
    "Message": "Some text in here...",
    "AffectedDate": "2015-10-30T07:00:00",
    "Type": "a",
    "CreatePeriod": "Older",
    "CreateDateString": "Monday, November 2, 2015 7:26 PM"
}];

$scope.checkCreatePeriod = function(value) {

    var result = false;
    angular.forEach($scope.data, function(obj) {

        if (obj['CreatePeriod'] === value && result === false) {
            result = true;
        }

    });

    return result;
}

在视图中:

<h3 ng-show="checkCreatePeriod('LastWeek')">Last Week</h3>