Search code examples
node.jsangularjsangularjs-scope

How can I get rid of the curly bracket from angularjs output and to clear the screen after the display


I thought I finally understand ng-repeat but now I do not know why the output include the curly bracket and how do I clear the screen after reading the output. Here is part of the output

{"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"}  

but what I really want is just the following without the curly bracket, the word title and the double quotes.

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

and after displaying the list of headlines, I want to clear the screen ( as in "cls" in the command prompt) my angularjs code is this

   $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);

My html is

   <div   ng-repeat="(key, value) in titles">    
    {{value}} 
    </div>

Solution

  • The syntax you are using is usually used to iterate over properties in an object. Since you already have an array, you can normally iterate over it and display the title value.

    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>