【问题标题】:How to test empty data in json file with angular如何使用角度测试json文件中的空数据
【发布时间】:2015-04-08 14:34:08
【问题描述】:

我想用angular测试json文件中的数据是否为空。

这是我的控制器:

app.controller('infosVillageController', ['$scope', function($scope) {
$scope.infosvillages = [
    {
        "name":"Soleil camp (Le)",
        "id":1,
        "website":"",
        "email":""
    },
    {
        "name":"Camp Cosse (La)",
        "id":80,
        "website":"",
        "email":""
    }
];
}]);

如您所见,有时我的 json 中没有数据。

在我的 html 中,我想显示数据是否为空。例如,我想显示“没有网站”或“没有电子邮件”。

那么,我如何测试我的 json 文件中的某些数据是否为空?

我在我的控制器中尝试过这样的事情,但它不起作用:

    if ($scope.infosvillages.website == null) {
        $scope.infosvillages.website = "no website"
    };

提前谢谢你

【问题讨论】:

  • 您发布的代码不会从 .json 文件中加载。
  • 为什么不比较空字符串"" 而不是null
  • @WayneEllery 抱歉,json 在控制器中
  • @RahilWazir 我也添加了这个测试

标签: json angularjs


【解决方案1】:

你可以这样做:

$scope.infosvillages.some(function(entry, i) {
    for(prop in entry){
        if(entry[prop] !== ""){
            console.log(entry[prop]);
            // or in your example
            entry[prop] = "no data found";
        }
    }
});

但是要做你想做的事,我相信你所要做的就是这样:

if ($scope.infosvillages.website === "") {
        $scope.infosvillages.website = "no website"
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2022-01-10
    • 2021-05-21
    • 2017-04-08
    • 2019-10-26
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多