http 요청에서 데이터를 가져 오는 사용자 지정 지시문을 만든 다음 템플릿을 통해 수신 된 데이터를 ng-repeat로 반복합니다.각도 지시어, http 요청 및 ng-repeat
나는 http 요청을 만들었지 만 지금은 붙어 있습니다. ng-repeat를 사용하여 템플릿의 데이터에 액세스하는 방법을 모릅니다.
내 코드 :
<test-dir category="posts"></test-dir>
<test-dir category="comments"></test-dir>
지침 : http://jsfiddle.net/g9zhdqw2/1/
나는 placeholer로 https://jsonplaceholder.typicode.com/을 사용하고 있습니다 :
여기angular.module("myApp")
.directive('testDir', ['$http', function($http) {
return {
restrict: 'AE',
replace: true,
template: '<div ng-repeat="item in data"><li>{{item.body}}</li></div',
scope: {
category: '@category'
},
controller: function($scope, $attrs) {
$http({
method: 'GET',
url: 'http://jsonplaceholder.typicode.com/' + $attrs.category + '/1'
}).then(function(result) {
$scope.data = result.data;
}, function(result) {
alert("Error: No data returned");
});
}
};
}]);
가 같은 코드로 jsfiddle입니다.
당신은 다음 링크를 http://stackoverflow.com/questions/16155542/dynamically-displaying-에 아마도이 질문에 대한 답을 읽어야입니다
ng-repeat
를 추가 완료하려면 template-in-ng-repeat-directive-in-angularjs –