C
C
comSaaxov2015-09-15 17:40:36
Angular
comSaaxov, 2015-09-15 17:40:36

$http and getting data from json?

Good afternoon, I started reading the angular.ru tutorial, making an example from the tutorial, everything works, I decided to change a little and ran into such a problem, there is a file:
main.json

[
  {
    "main_view" : 10000,
    "main_view_one" : 20000,
    "main_view_two" : 30000
  }
]

controller.js
angular
  .module('app',[])
  .controller('MainList', MainList);
function MainList ($scope, $http) {
  $http.get('main.json').success(function(mydata){
  	console.log(mydata);
    $scope.main = mydata;
    
  });
}

I write the ngController directive in the body, do the binding :
<p>{{main_view}}</p>
but nothing is output. why?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
comSaaxov, 2015-09-16
@comSaaxov

thanks to everyone for the tips, I figured it out, it was necessary to parse json
, it turned out like this:

$scope.result = angular.fromJson(mydata);
$scope.main_view = $scope.result[0].main_view;

P
Pavel Ivanov, 2015-09-15
@eastywest

In general, it's better to use then, because success has been deprecated.

V
Valentin Dubrovsky, 2015-09-15
@matroskin13

Because what you received in response to ajax lies in this $scope.main variable. That is, in the template you need to do this - {{main}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question