Javascript 以ng repeat显示字符串输出
我想在视图中显示json对象。代码是:Javascript 以ng repeat显示字符串输出,javascript,json,angularjs,Javascript,Json,Angularjs,我想在视图中显示json对象。代码是: <ul ng-repeat="item in items"> <li ng-repeat="(key, val) in item"> {{key}}: {{val}} </li> </ul> 在console.log之后,我得到了 [{"user_name": "Pranav", "user_id": 1}, {"user_name": "Sagar", "user_id":
<ul ng-repeat="item in items">
<li ng-repeat="(key, val) in item">
{{key}}: {{val}}
</li>
</ul>
在console.log之后,我得到了
[{"user_name": "Pranav", "user_id": 1}, {"user_name": "Sagar", "user_id": 2}]
我不能像前面的例子那样操纵它
如何将其转换为以下格式:
$scope.items =
[
{"user_name": "Pranav", "user_id": 1},
{"user_name": "Sagar", "user_id": 2}]
];
因此,我可以使用它。您的数据格式正确,但是,对于AJAX请求,请使用Angular的
$http
,因为这将触发摘要周期并允许视图更新:
$http.get("http://127.0.0.1:8000/user_list/").success(function(data) {
$scope.items = data;
});
我在这里创作了一把小提琴:
我简化了示例中的HTTPGET调用,因为获取JSON没有问题。下面是代码示例
<div ng-app="myApp">
<div ng-controller="TextController">
<div>
<label for="spSelectViewMenu">Please select the list to view:</label>
<select id="spSelectViewMenu" ng-model="list" ng-options="c.user_name for c in lists"></select><br />
<ul ng-show="list" ng-repeat="(key, val) in list" >
<li>{{key}} : {{val}}</li>
</ul>
</div>
</div>
</div>
<script>
var myAppModule = angular.module('myApp', []);
myAppModule.controller('TextController', function ($scope) {
$scope.lists = JSON.parse('[{"user_name": "Pranav", "user_id": 1}, {"user_name": "Sagar", "user_id": 2}]');
});
</script>
请选择要查看的列表:
- {{key}}:{{val}
var myAppModule=angular.module('myApp',[]);
myAppModule.controller('TextController',函数($scope){
$scope.lists=JSON.parse('[{“用户名”:“Pranav”,“用户名”:1},{“用户名”:“Sagar”,“用户名”:2}');
});
<div ng-app="myApp">
<div ng-controller="TextController">
<div>
<label for="spSelectViewMenu">Please select the list to view:</label>
<select id="spSelectViewMenu" ng-model="list" ng-options="c.user_name for c in lists"></select><br />
<ul ng-show="list" ng-repeat="(key, val) in list" >
<li>{{key}} : {{val}}</li>
</ul>
</div>
</div>
</div>
<script>
var myAppModule = angular.module('myApp', []);
myAppModule.controller('TextController', function ($scope) {
$scope.lists = JSON.parse('[{"user_name": "Pranav", "user_id": 1}, {"user_name": "Sagar", "user_id": 2}]');
});
</script>