1

I receive from my rest api news with the specified id. When I display all news I used ng-repeat and works fine but when I want display one object this method is not working.

My .when code:

.when('/displaynews/:id',{
    templateUrl: 'views/display.html',
    controller: 'NewsDisplayController',
    constollerAs: 'displaydash'
})

and the controller:

.controller('NewsDisplayController',
    function($routeParams, NewsModel){

      var displaydash = this;
      var newsId = $routeParams.id;
      path = 'getNewsById/'+newsId;

      function getNewsById() {
          NewsModel.getNewsById().then(function (result){
              displaydash.news = result.data;
              console.log(displaydash.news);
          })
      }

      getNewsById();
})

Result from console.log:

Object { id="56f1ba6b275c8aa5bf4895d8",  title="Tytul",  text="Text",  more...}

How can I display this in my html template?

I try to display in html file in this way:

<p>{{news.title}}</p>
<p>{{news.text}}</p>

But it's not working

5
  • what have you tried? Looks like it will just work to bind to news, correct? Commented Mar 23, 2016 at 9:43
  • Do you want to show the JSON as a JSON string, or show the content out of it? Commented Mar 23, 2016 at 9:44
  • But it's not working how? do you get an error? Commented Mar 23, 2016 at 9:49
  • No, no errors. Just blank page. Commented Mar 23, 2016 at 9:52
  • Have you tried to bind anything to the template. Try a hard coded property of displaydash. Commented Mar 23, 2016 at 9:54

2 Answers 2

2

You can go for :

angular.toJson(JSONObj);

So, here you can go for: in Controller:

displaydash.news = result.data;
$scope.news      = angular.toJson(displaydash.news);

in HTML:

<p>{{news}}</p>

The issue in your question is simple, you are trying to access news object which you have not defined, try creating a scope variable for it, you will be easily able to access it:

 $scope.displaydash.news = result.data;

<p>{{displaydash.news.title}}</p>
<p>{{displaydash.news.text}}</p>

Refer: https://docs.angularjs.org/api/ng/function/angular.toJson

Sign up to request clarification or add additional context in comments.

9 Comments

the OP is attempting to use the controller as method of binding. So, no $scope required.
'OP'? What's that?
Original Post. You will come across abbreviation here on SO a lot.
but in angular you require a $scope variable to be able to bind the value , that is just incorrect. In fact even the documented example on the angular docs now uses controller as syntax: docs.angularjs.org/api/ng/directive/ngController
You might find this interesting as well: toddmotto.com/digging-into-angulars-controller-as-syntax
|
1

If result.data is an object, enclose it with square brackets and set as news, otherwise use it directly.

 displaydash.news = typeof(result.data) == "object"?[result.data]: result.data;

4 Comments

why turn it into an array?
lukassz is using ng-repeat and the code works fine when there are multiple objects. So converting the single object to an array should work.
@Gilsha, good point, but the api will be returning an array structure right? Even if we have just one object?
Did you see the console.log output in the question? It shows an object and not an array.So assuming getNewsById API returns a single object. lukassz may be using some other API for loading multiple news items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.