Javascript ng:重复不加载数据
这是我的控制器从服务器获取数据的功能Javascript ng:重复不加载数据,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,这是我的控制器从服务器获取数据的功能 function carsController($http, $scope, $timeout) { var vm = this; vm.getCarData = getCarData; function getCarData(){ $http.get('/api/getData').then(function (response) { console.log(response.data.mes
function carsController($http, $scope, $timeout) {
var vm = this;
vm.getCarData = getCarData;
function getCarData(){
$http.get('/api/getData').then(function (response) {
console.log(response.data.message);
vm.list = response.data.message;
});
}
}
这是返回的数据
{
"message":[
{
"emp_id":1,
"emp_name":"toyota",
"city":"city1",
"nic_no":4554
},
{
"emp_id":2,
"emp_name":"sunny",
"city":"city2",
"nic_no":57412
},
{
"emp_id":3,
"emp_name":"tata",
"city":"city3",
"nic_no":1234
}
]
}
和html代码来显示数据。我正在使用汽车控制器作为汽车
<div class="row" data-ng-init="cars.getCarData()">
<div class="panel panel-default">
<table class="table table-bordered table-hover">
<tr>
<th>Name</th>
<th>Pages</th>
</tr>
<tr ng:repeat="vehicle in cars.list track by $index">
<td>{{vehicle.emp_name}}</td>
<td>{{vehicle.city}}</td>
</tr>
</table>
</div>
</div>
您确定问题不是因为您使用了两个不同的对象
vm.list
用于从$http
接收数据并使用cars.list进行ng repeat
将数据存储到$scope.vm.list
在UI中使用
ng repeat=“vm.list中的汽车按$index跟踪”问题在于,正如您在发布的控制台日志中所看到的,response.data.message是一个对象。不是数组。
试试这个
vm.list = response.data.message.message;
下面将绑定消息数组您有两个问题
您正在将vm.list
分配给response.data.message
,正如NJ_93所指出的,它是一个对象。使用
vm.list = response.data.message.message;
您没有从控制器调用getCarData()函数。因此,永远不会提取数据
正如泰国指出的->语法是ng repeatng:repeat
也很好用。非常鲜为人知的事实车内车辆。list
可能是车内车辆。vm.list
您是如何在控制器中定义vm的?vm.list=response.data.message.message
尝试过此操作。然后我只得到了列标题。没有行。好的,这里有一个快速的hack,您可以看到数据是如何绑定到视图的,在html put{{cars.list}中的ng repeat上方,并查看它显示了什么。它显示了我在问题中添加的相同输出。尝试输出{cars.list.message}它是不打印的。请检查问题的更新部分。他正在使用controllerAs。在这里,您没有在控制器中使用$scope,它已经被注入。
vm.list = response.data.message.message;