angularjs - Getting data from a web service with Angular.js -


im trying data in json format remote ws using angular , im having trouble. data comes web service correctly cant use inside controller. why that? angular code:

var booksjson; var app = angular.module('booksinventoryapp',[]);  // data ws app.run(function ($http) {     $http.get("https://some_api_path").success(function (data) {         booksjson = data;         console.log(data);  //working     }); });  app.controller('booksctrl', function ($scope) {      $scope.data = booksjson;     console.log($scope.data); //not working }); 

html:

<section ng-controller="booksctrl"> <h2 ng-repeat="book in data">{{book.name}}</h2> </section> 

you should put $http.get inside controller.

also, web service returns object not array. ng-repeat should this: book in data.books

here working example:

var app = angular.module('booksinventoryapp', []);    app.controller('booksctrl', function($scope, $http) {      $http.get("https://whispering-woodland-9020.herokuapp.com/getallbooks")      .then(function(response) {        $scope.data = response.data;      });  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>  <article ng-app="booksinventoryapp">    <section ng-controller="booksctrl">      <h2 ng-repeat="book in data.books">{{book.name}}</h2>        </section>  </article>


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -