1

iam newbie and i want to ask.

i want to get some value from JSON API (title, course_id, etc), and put the Value to my Template with Directive. so at Index, i can repeat my tempalate with data from the API.

How to get Value from that JSON ?

This my Code :

my API

{
"count": 1,
"next": null,
"previous": null,
"results": [
    {
        "url": "http://192.168.1.37:8000/api/courses/a/",
        "title": "I Love You",     <-- I want to put this Value to my Template
        "course_id": "1",
        "starting_date": "2016-10-03"
    }
           ]
}

Controller demo.js

demo.controller("demo", function($scope, $http) {
$http.get('http://192.168.1.37:8000/api/courses/'). <-- Data From my API
    then(function(response) {
        $scope.courses = response.data;
    });


    });

demo.directive("demoItemDirective", function() {
   return {
        scope : { demoInfo : "=info"},
        templateUrl : "/app/demo.tmpl"   <-- My Template
    };
   });

My Template demo.tmpl

<p>{{demoInfo.count}}</p>                <-- Works, count Displayed
<p>{{demoInfo.results.title}</p>         <-- Not works, title not displayed

My Index.html

   <div ng-repeat="group in courses | groupCount:2">
   <div ng-repeat="x in group">
        <demo-item-directive info="x"></demo-item-directive>
   </div>
   </div>

2 Answers 2

3

It should be

<p>{{demoInfo.results[0].title}</p>  

Your result contain an Array of object.

You need to access with index.

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

Comments

0

According to your post, the result is an array:

"results": [
    {
        "url": "http://192.168.1.37:8000/api/courses/a/",
        "title": "I Love You",     <-- I want to put this Value to my Template
        "course_id": "1",
        "starting_date": "2016-10-03"
    }
]

Thus, you can't refer to the title itself, you have to reference the first element:

{{demoInfo.results[0].title}}

1 Comment

Thx mate, thats Work !

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.