如何摆脱angularjs输出中的花括号并在显示后清除屏幕

问题描述 投票:0回答:1

我以为我终于了解了ng-repeat,但是现在我不知道为什么输出中包含大括号,并且在读取输出后如何清除屏幕。这是输出的一部分

{"title":"NFL Draft 2020: Over 50 prospects will take part in 'virtual' interviews to air during the event, per report - CBS Sports"} 
{"title":"Illinois governor says feds sent wrong type of protective medical masks - CNN"}  

但是我真正想要的是以下内容,没有大括号,单词标题和双引号。

NFL Draft 2020: Over 50 prospects will take part in 'virtual' interviews to air during the event, per report - CBS Sports

并且在显示标题列表之后,我想清除屏幕(如命令提示符中的“ cls”所示)我的angularjs代码是这个

   $http.post('/disdata', " ").then(function(response) {
    $scope.answer = response.data;
    var titles = []; 
    for (var i = 0; i < $scope.answer.length; i++) {
    titles.push ({  
    title: $scope.answer[i].title 
    });
    };
    $scope.titles = titles;
    console.log($scope.titles);

我的html是

   <div   ng-repeat="(key, value) in titles">    
    {{value}} 
    </div>
node.js angularjs angularjs-scope
1个回答
0
投票

您正在使用的语法通常用于遍历对象中的属性。由于您已经有了一个数组,因此通常可以对其进行迭代并显示title值。

angular.module('app', []).controller('Ctrl', ['$scope', ($scope) => {
  $scope.titles = [{
      "title": "NFL Draft 2020: Over 50 prospects will take part in 'virtual' interviews to air during the event, per report - CBS Sports"
    },
    {
      "title": "Illinois governor says feds sent wrong type of protective medical masks - CNN"
    }
  ];
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<body ng-app="app" ng-controller="Ctrl">
  <div ng-repeat="title in titles">
    {{title.title}}
  </div>
</body>
© www.soinside.com 2019 - 2024. All rights reserved.