简体   繁体   English

使用Angularjs从Json获取动态数据到Html

[英]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". 当我写33变量时,它的工作原理是“response.data”。 It isn't working. 它不起作用。 How can I get the value of my JSON. 我怎样才能获得我的JSON的价值。

This is my JSON page 这是我的JSON页面

[{"value":"23"}]

I have to get this 23 我必须得到这个23

Thanks. 谢谢。

Try this code, You need to access the index[0] of the response 试试这段代码,你需要访问响应的index[0]

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

Like this.You have JSON response 像这样。你有JSON回应

[{"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 . 尝试使用response.data[0].value获取它。

Maybe you have to JSON.parse(response.data)[0].value to get it. 也许你必须得到JSON.parse(response.data)[0].value才能得到它。

In $http service calls , data response is usually in this format $http服务调用中,数据响应通常采用这种格式

在此输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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