Angular无法将json内容返回给对象

时间:2014-09-01 14:23:39

标签: javascript json angularjs

我正在尝试将json文件内容提供给对象

var myApp = angular.module('AngularPortfolio', []);

myApp.config( function($interpolateProvider) {
    $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});

myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http){
    $scope.projects = {};
    $http.get('../bundles/xxxxxxxxxx/portfolio/data.json')
    .success( function(data) {
        $scope.projects = data;
    })
    .error( function(data) {
        console.log('ERREUR');
    });
}]);

(编辑最新帖子) 我的JSON文件:

[
    {
        "title": "example 01",
        "img" : "1.jpg",
        "filters" : {
            "accompagner" : "communication corporate",
            "solutions" : "digital"
        }
    },
    {
        "title": "example 02",
        "img" : "2.jpg",
        "filters" : {
            "accompagner" : "communication financiere",
            "solutions" : "strategies et dispositifs"
        }
    },
    {
        "title": "example 03",
        "img" : "3.jpg",
        "filters" : {
            "accompagner" : "communication rse et developpement durable",
            "solutions" : "design et identite"
        }
    }
]

(编辑最新帖子)和我的ng-repeat代码:

<div ng-repeat="item in projects">
          <img src="../bundles/xxxxxxxx/uploads/{[{item.img}]}" alt="{[{item.title}]}">
          <h3>{[{item.title}]}</h3>
        </div>

此代码返回数据字符串,我不能将其用于ng-repeat。 你能救我吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

取决于您的共享JSON文件,如果这是您的最终JSON输出,如果不是,请分享您在控制器中获得的最终JSON

<div ng-repeat="item in projects track by $index">
          <img ng-src="../bundles/xxxxxxxx/uploads/{{item.img}}" alt="{{item.title}}">
          <h3>{{item.title}}</h3>
        </div>