Angular的小写,并将json数据中的空格替换为破折号

Angular lowercase and replace spaces with dashes in json data

本文关键字:空格 替换 破折号 数据 json 并将 Angular      更新时间:2023-09-26

我的数据如下:

{
  "statusCode": 200,
  "result": {
    "items": [
      {
        "Name": "date",
        "Fields": {
          "{3C3170EE-E6D5-4075-A864-8AB86D1E8E98}": {
            "Name": "Promo Content",
            "Value": "September 22, 2015"
          }
        }
      },
      {
        "Name": "rate",
        "Fields": {
          "{3C3170EE-E6D5-4075-A864-8AB86D1E8E98}": {
            "Name": "Promo Content",
            "Value": "10%"
          }
        }
      },
      {
        "Name": "description",
        "Fields": {
          "{3C3170EE-E6D5-4075-A864-8AB86D1E8E98}": {
            "Name": "Promo Content",
            "Value": "This rate is good as of the date listed above."
          }
        }
      }
    ]
  }
}

这是我的HTML和JS:

<body ng-app="myApp">
<div ng-controller="CallWebApi">
    <ul>
        <li ng-repeat="item in data">
            {{ item.Name }}: {{ item.Fields["{3C3170EE-E6D5-4075-A864-8AB86D1E8E98}"].Value }}
        </li>
    </ul>
</div>
<script>
angular.module('myApp',[]).controller('CallWebApi', function($scope, $http) {
    // Local version of the data
    $http.get('./test.js').
        success(function (data) {
            $scope.data = data.result.items;
            console.log('success ' + data)
        })
        .error(function(data) {
            console.log('failure ' + data)
        });
});
</script>
</body>

如何用小写字母写出描述,并用破折号代替空格?

我希望:

  • 日期:2015年9月22日
  • 率:10%
  • 描述:this-rate-is-good-as-of-the-date-listed-above。

quertyking,

创建自定义过滤器
var str = text.replace(/'s+/g, '-');
return str.toLowerCase();

使用JavaScript函数replace("space", "underscore")lowercase过滤器来解决您的问题。

<ul>
    <li ng-repeat="item in data">
        {{ item.Name }}: {{ item.Fields["{3C3170EE-E6D5-4075-A864-8AB86D1E8E98}"].Value.replace(" ","_") | lowercase }}
    </li>
</ul>