Angular ng-repeat中的Json字符串不起作用

时间:2021-04-24 20:33:37

I get a json string from an $http request, and I want to use ng-repeat to display the data. As it stands, I get the json string from the server, but ng-repeat is not putting the data on the dom.

我从$ http请求中获取了一个json字符串,我想使用ng-repeat来显示数据。就目前而言,我从服务器获取了json字符串,但是ng-repeat并没有将数据放在dom上。

<div ng-controller="mycontroller2">
    <form ng-submit="submit()">
    {% csrf_token %}
       Search:<input ng-model="artiste" />
        <input type="submit" value="submit" />
    </form>
    <table ng-repeat="artist in artists">
        <tr>
            <td> {({ artist.fields.link })} </td>
        </tr>
    </table>
</div>
<script>
artApp.controller('mycontroller2', ['$scope', '$http',
function($scope, $http){
    $scope.submit = function(){
    var call = {
        method: 'POST',
        url: '/rest/',
        data: {
            "artiste": $scope.artiste
        },
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFTOKEN': "{{ csrf_token }}"
        }
    };
    $http(call)
        .success(function(data){
            $scope.artists = data;
        })
    }
}]);

The response data that shows up in devtools is a json string

在devtools中显示的响应数据是一个json字符串

"[{\"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \"https://trove2.storage.googleapis.com/leonardo-da-vinci/galloping-rider-and-other-figures.jpg\", \"title\": \"Galloping Rider and other figures\"}, \"model\": \"serve.art\", \"pk\": 63057},

I thought I could just iterate over it with ng-repeat and generate html elements with the data, but it doesn't work right now.

我想我可以用ng-repeat迭代它并用数据生成html元素,但它现在不起作用。

3 个解决方案

#1


1  

For Json Parsing in angular you have to use the following code in success method.

对于角度为Json的Parsing,您必须在success方法中使用以下代码。

.success(function(data){
    var response=angular.fromJson(data);

    $scope.artists = response.fields;
 })  

modify your <table> as below, Because you have assign the response array to $scope.artists.
Then you can use your json data with individual key.

修改您的

,如下所示,因为您已将响应数组分配给$ scope.artists。然后,您可以将json数据与单个密钥一起使用。
<table ng-repeat="artist in artists">
    <tr>
        <td>{{artist.link}}</td>
    </tr>
</table>

check whether the value is getting or not in console.

检查控制台中是否有值。

.success(function(data){
    var response=angular.fromJson(data);

    $scope.artists = response.fields;
    console.log("artist :"+$scope.artists.artist);
    console.log("link :"+$scope.artists.link);
 })

#2


0  

Shouldn't JSON.parse just work?

JSON.parse不应该正常工作吗?

$scope.artists = JSON.parse(data);

#3


0  

You have an error in your html, try :

您的html中有错误,请尝试:

 <td> {({ fields.artist.link.})} </td>

instead of artist.fields.link, according to your json : \"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \

而不是artist.fields.link,根据你的json:\“fields \”:{\“artist \”:\“Leonardo da Vinci \”,\“link \”:\

#1


1  

For Json Parsing in angular you have to use the following code in success method.

对于角度为Json的Parsing,您必须在success方法中使用以下代码。

.success(function(data){
    var response=angular.fromJson(data);

    $scope.artists = response.fields;
 })  

modify your <table> as below, Because you have assign the response array to $scope.artists.
Then you can use your json data with individual key.

修改您的

,如下所示,因为您已将响应数组分配给$ scope.artists。然后,您可以将json数据与单个密钥一起使用。
<table ng-repeat="artist in artists">
    <tr>
        <td>{{artist.link}}</td>
    </tr>
</table>

check whether the value is getting or not in console.

检查控制台中是否有值。

.success(function(data){
    var response=angular.fromJson(data);

    $scope.artists = response.fields;
    console.log("artist :"+$scope.artists.artist);
    console.log("link :"+$scope.artists.link);
 })

#2


0  

Shouldn't JSON.parse just work?

JSON.parse不应该正常工作吗?

$scope.artists = JSON.parse(data);

#3


0  

You have an error in your html, try :

您的html中有错误,请尝试:

 <td> {({ fields.artist.link.})} </td>

instead of artist.fields.link, according to your json : \"fields\": {\"artist\": \"Leonardo da Vinci\", \"link\": \

而不是artist.fields.link,根据你的json:\“fields \”:{\“artist \”:\“Leonardo da Vinci \”,\“link \”:\