我从api返回结果 [{姓名:“ABC”,年龄:20},{姓名:“DEF”,年龄:25}]
$ scope.names设置为从api返回的数组。在浏览器中,它会打印2行,但html表行中的每个单元格都是空的。为什么呢?
--net=host
答案 0 :(得分:1)
不是取整个(键,值)对,而是只取值。
HTML:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr ng-repeat="val in names">
<td>{{val.name}}</td>
<td>{{val.age}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope){
$scope.names =[{name:"ABC", age: 20}, {name: "DEF", age: 25}]
});
</script>
</body>
</html>
答案 1 :(得分:0)
在控制器中你有这样的东西
function CTRL($scope) {
$scope.names= [
{name: "ABC", age: 20}, {name: "DEF", age: 25}];
}
这是一个对象数组,你需要循环它,如下所示
<tr ng-repeat="(key,value) in names">
<td>{{ value.name }}</td>
<td>{{ value.age }}</td>
</tr>
检查以下内容以进行演示
angular.module("myApp", []).controller(CTRL);
function CTRL($scope) {
$scope.names = [
{name: "ABC", age: 20}, {name: "DEF", age: 25}];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="CTRL">
<table>
<tr ng-repeat="(key,value) in names">
<td>{{ value.name }}</td>
<td>{{ value.age }}</td>
</tr>
</table>
</div>
答案 2 :(得分:0)
你可以这样做:
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table>
<tr>
<td>{{name of names}}</td>
<td>{{age of names}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope){
$scope.names =[{name:"ABC", age: 20}, {name: "DEF", age: 25}]
});
</script>
</body>
</html>
答案 3 :(得分:0)
你的代码似乎对我有用,不确定你在控制器中正确绑定
angular.module("myApp", []).controller(CTRL);
function myCtrl($scope) {
$scope.names = [{name: "ABC", age: 20}, {name: "DEF", age: 25}];
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myCtrl">
<table>
<tr ng-repeat="name in names">
<td>{{ name.name }}</td>
<td>{{ name.age }}</td>
</tr>
</table>
</div>
&#13;