我在想如何将多个.json文件加载到模板中。 在我的示例中,我有来自用户的提交内容,如果可能,我不想将所有内容都存储在一个json文件中。如何以角度加载多个json文件
如何从多个json文件中抓取一些数据?
这里是我的plunk example
我想加载users.json以及
var app = angular.module('myApp', []);
app.directive('contentItem', function ($compile,$parse) {
templates = {
image: 'image.html',
event: 'event.html',
article: 'article.html',
ad: 'ad.html',
discount: 'discount.html',
video: 'video.html'
}
var linker = function(scope, element, attrs) {
scope.setUrl = function(){
return templates[scope.content.content_type];
}
}
return {
restrict: "E",
replace: true,
link: linker,
scope: {
content: '='
},
templateUrl: 'main.html'
};
});
function ContentCtrl($scope, $http) {
"use strict";
$scope.url = 'content.json';
$scope.content = [];
$scope.fetchContent = function() {
$http.get($scope.url).then(function(result){
$scope.content = result.data;
});
}
$scope.fetchContent();
}
帮助表示赞赏
你可以有一个http调用。你不希望出现这种情况? – V31 2014-09-02 06:46:47
你能证明一下吗? – 2014-09-02 07:05:32