简体   繁体   中英

Getting dynamic data from Json to Html with Angularjs

var app = angular.module('myApp', ['ridge-speedometer']);
app.controller('customersCtrl', function($scope, $http, $timeout) {
    $scope.getData = function(){
    $http.get('asd.php').then(function (response) {
        $scope.myData = 33; ////////// response.data
    });
};

// Function to replicate setInterval using $timeout service.
$scope.intervalFunction = function(){
    $timeout(function() {
        $scope.getData();
        $scope.intervalFunction();
    }, 1000)
};

// Kick off the interval
$scope.intervalFunction();

Hi guys, as you see the codes above, I just have problem that line:

$scope.myData = 33; 
$scope.myData = response.data" 

when I'm writing 33 to the variable it works, but with "response.data". It isn't working. How can I get the value of my JSON.

This is my JSON page

[{"value":"23"}]

I have to get this 23

Thanks.

Try this code, You need to access the index[0] of the response

$scope.getData = function(){
    $http.get('asd.php').then(function (response) {
      $scope.myData = response.data[0].value;
});

Like this.You have JSON response

[{"value":"23"}]

Get value

$scope.myData = response[0].value;

See Demo

 var response= '[{"value":"23"}]'; object = JSON.parse(response); //parses your json into object console.log(object[0].value); 

Try get it with response.data[0].value .

Maybe you have to JSON.parse(response.data)[0].value to get it.

In $http service calls , data response is usually in this format

在此输入图像描述

I suggest you to get data like this

var response = response.data.data;

// now you can assign like you are doing
$scope.myData = response.value;

// or if your response is an array
$scope.myData = response[0].value;

I hope this helps. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM