AngularJS에서 ng-repeat을 사용하여 키와 값을 반복하려면 어떻게 해야 합니까?
컨트롤러에는 다음과 같은 데이터가 있습니다.$scope.object = data
이제 이 데이터는 사전입니다. 키와 값은 사전에서json
.
Atribute에 액세스 할 수 있는 것은,object.name
템플릿에 추가되어 있습니다.키를 반복해서 테이블에 표시할 수 있는 방법이 있나요?
<tr><td> {{key}} </td> <td> data.key </td>
데이터는 다음과 같습니다.
{
"id": 2,
"project": "wewe2012",
"date": "2013-02-26",
"description": "ewew",
"eet_no": "ewew",
}
그럼 어떻게 해?
<table>
<tr ng-repeat="(key, value) in data">
<td> {{key}} </td> <td> {{ value }} </td>
</tr>
</table>
이 방법은 다음 문서에 기재되어 있습니다.https://docs.angularjs.org/api/ng/directive/ngRepeat
양방향 바인딩을 사용하여 속성 값을 편집하려는 경우:
<tr ng-repeat="(key, value) in data">
<td>{{key}}<input type="text" ng-model="data[key]"></td>
</tr>
angular에 내장된 함수는 없다고 생각합니다만, 모든 헤더 이름을 포함하는 별도의 스코프 속성을 생성하여 다음과 같이 이 속성을 자동으로 채울 수 있습니다.
var data = {
foo: 'a',
bar: 'b'
};
$scope.objectHeaders = [];
for ( property in data ) {
$scope.objectHeaders.push(property);
}
// Output: [ 'foo', 'bar' ]
키 값이 알파벳 순서로 표시되지 않도록 하기 위해 다음 절차를 따를 수 있습니다.
자바스크립트
$scope.data = {
"id": 2,
"project": "wewe2012",
"date": "2013-02-26",
"description": "ewew",
"eet_no": "ewew",
};
var array = [];
for(var key in $scope.data){
var test = {};
test[key]=$scope.data[key];
array.push(test);
}
$scope.data = array;
HTML
<p ng-repeat="obj in data">
<font ng-repeat="(key, value) in obj">
{{key}} : {{value}}
</font>
</p>
오브젝트 위에 루프하는 ToDo 리스트의 예ng-repeat
:
var app = angular.module('toDolistApp', []);
app.controller('toDoListCntrl', function() {
var self = this;
self.toDoListItems = {};// []; //dont use square brackets if keys are string rather than numbers.
self.doListCounter = 0;
self.addToDoList = function() {
var newToDoItem = {};
newToDoItem.title = self.toDoEntry;
newToDoItem.completed = false;
var keyIs = "key_" + self.doListCounter++;
self.toDoListItems[keyIs] = newToDoItem;
self.toDoEntry = ""; //after adding the item make the input box blank.
};
});
app.filter('propsCounter', function() {
return function(input) {
return Object.keys(input).length;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="toDolistApp">
<div ng-controller="toDoListCntrl as toDoListCntrlAs">
Total Items: {{toDoListCntrlAs.toDoListItems | propsCounter}}<br />
Enter todo Item: <input type="text" ng-model="toDoListCntrlAs.toDoEntry"/>
<span>{{toDoListCntrlAs.toDoEntry}}</span>
<button ng-click="toDoListCntrlAs.addToDoList()">Add Item</button> <br/>
<div ng-repeat="(key, prop) in toDoListCntrlAs.toDoListItems">
<span>{{$index+1}} : {{key}} : Title = {{ prop.title}} : Status = {{ prop.completed}} </span>
</div>
</div>
</body>
완전한 예는 다음과 같습니다.-
<!DOCTYPE html >
<html ng-app="dashboard">
<head>
<title>AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link rel="stylesheet" href="./bootstrap.min.css">
<script src="./bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
</head>
<body ng-controller="myController">
<table border='1'>
<tr ng-repeat="(key,val) in collValues">
<td ng-if="!hasChildren(val)">{{key}}</td>
<td ng-if="val === 'string'">
<input type="text" name="{{key}}"></input>
</td>
<td ng-if="val === 'number'">
<input type="number" name="{{key}}"></input>
</td>
<td ng-if="hasChildren(val)" td colspan='2'>
<table border='1' ng-repeat="arrVal in val">
<tr ng-repeat="(key,val) in arrVal">
<td>{{key}}</td>
<td ng-if="val === 'string'">
<input type="text" name="{{key}}"></input>
</td>
<td ng-if="val === 'number'">
<input type="number" name="{{key}}"></input>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
<script type="text/javascript">
var app = angular.module("dashboard",[]);
app.controller("myController",function($scope){
$scope.collValues = {
'name':'string',
'id':'string',
'phone':'number',
'depart':[
{
'depart':'string',
'name':'string'
}
]
};
$scope.hasChildren = function(bigL1) {
return angular.isArray(bigL1);
}
});
</script>
</html>
Use below code it is working to display your key and value here is key start with 1:
<tr ng-repeat="(key,value) in alert_list" >
<td>{{key +1}}</td>
<td>{{value.title}}</td>
</tr>
Below is document link for it.
https://docs.angularjs.org/api/ng/directive/ngRepeat
javascript(컨트롤러) 또는 html(각도 보기)에서 할 수 있습니다.
js:
$scope.arr = [];
for ( p in data ) {
$scope.arr.push(p);
}
html:
<tr ng-repeat="(k, v) in data">
<td>{{k}}<input type="text" ng-model="data[k]"></td>
</tr>
html 방식이 좀 더 각진 것 같습니다만, 컨트롤러로 할 수도 있고 html로 검색할 수도 있습니다.
오브젝트 키를 참조하는 것도 나쁘지 않습니다.필요한 경우 키 배열이 제공됩니다.자세한 내용은 이쪽:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
언급URL : https://stackoverflow.com/questions/15127834/how-to-iterate-over-the-keys-and-values-with-ng-repeat-in-angularjs
'programing' 카테고리의 다른 글
부모 클래스에서 자식 구성 요소 메서드 호출 - 각도 (0) | 2023.03.12 |
---|---|
재스민 스파이의 반품가치는 어떻게 변경하나요? (0) | 2023.03.12 |
React.js - 정의되지 않은 속성을 읽을 수 없습니다. (0) | 2023.03.12 |
URL용 PHP 검증/regex (0) | 2023.02.01 |
재귀 ORM 클래스와 관련된 스프링 저장소 성능 문제 (0) | 2023.02.01 |