-1

I'm learning basics of Angular and have a problem with getting data form simple rest api. I read this: How to write an angularJs Controller to GET Rest Data from Parse.com

And still don't know what is wrong in this code: Controller:

Todoapp.controller('DaneAPI' ['$scope', '$http', function($scope, $http) {
        $scope.items = [];
        $scope.getItems = function() {
            $http({
                method : 'GET',
                url : '/todos'
            }).then(function(data, status) {
                    $scope.items = data;
                }, function(data, status) {
                    alert("Error");
                    }
            );

    }}]);

And in html (to be more precise its .ejs cause I'm using express, but that shouldn't matter):

<body ng-controller="TodosCtrl as todos">
  <ul clas="nav nav-pills">
    <li> <a href ng-click="tab = 'daneZapi'">DaneZApi</a></li>
  </ul>
  <div class="panel" ng-show="tab === 'daneZapi'" ng-controller="DaneAPI">
    <button type="button" ng-click="getItems()">Get Items</button>
    <ul>
      <li ng-repeat="item in items.name">
        {{item.name}}
      </li>
    </ul>
  </div>
</body>

GET send to localhost:3000/todos works ok - I testes it with Postman and it's giving me some data.

And one more question that I can't figure out - why does ng-show does not work for <button>? It's visible despite what is in "tab". In full listing I have 3 tabs.

Community
  • 1
  • 1
wiwo
  • 721
  • 1
  • 13
  • 18
  • Why is `item in items.name` not `item in items`? copy paste error? typo? not understand ng-repeat? or is it doing something i just don't understand. – Kevin B Dec 01 '15 at 21:56
  • what does your response look like in the browser? – Sari Rahal Dec 01 '15 at 21:57
  • sorry for the copy-paste error! I was testing it with a few possibilities... i tried with item in items - still nothign The response for this get should have two name values. In the browser I don't see anything – wiwo Dec 01 '15 at 21:59
  • `ng-show="tab === 'daneZapi'"`, do you have a variable like `$scope.daneZapi = true/false` in `TodosCrtl`? – Shaohao Dec 01 '15 at 22:01
  • there is a variable in `DaneZApi` – wiwo Dec 01 '15 at 22:03

1 Answers1

0

Not sure if you already have defined ng-app (e.g. on your html tag), but if you haven't you need to add that. E.g.:

<html ng-app="app">...

You also need a corresponding module definition. :-)

Fredrik Holm
  • 166
  • 9
  • I have `` and in .js i have `var Todoapp = angular.module('Todoapp', ['ngRoute', 'ngResource']);` – wiwo Dec 01 '15 at 22:01