-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.

5
  • 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. Commented Dec 1, 2015 at 21:56
  • what does your response look like in the browser? Commented Dec 1, 2015 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 Commented Dec 1, 2015 at 21:59
  • ng-show="tab === 'daneZapi'", do you have a variable like $scope.daneZapi = true/false in TodosCrtl? Commented Dec 1, 2015 at 22:01
  • there is a variable in <a href ng-click="tab = 'daneZapi'">DaneZApi</a> Commented Dec 1, 2015 at 22:03

1 Answer 1

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. :-)

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

1 Comment

I have <html ng-app="Todoapp"> and in .js i have var Todoapp = angular.module('Todoapp', ['ngRoute', 'ngResource']);

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.