Javascript ng重复不工作angularjs 1.6
这是我在index.html中使用的组件gamesGrid的模板。在这个模板里面,我有一个表,我想在上面使用ng repeat,但它不起作用Javascript ng重复不工作angularjs 1.6,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,这是我在index.html中使用的组件gamesGrid的模板。在这个模板里面,我有一个表,我想在上面使用ng repeat,但它不起作用 <div class="container"> <table class="table table-hover"> <thead> <tr> <th>Title</th> <th>Platform
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Platform</th>
<th>Score</th>
<th>Genre</th>
<th>Editor's Choice</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="detail in gamesDetails">
<td>{{detail.title}}</td>
</tr>
</tbody>
</table>
</div>
这是我的controller.js文件
angular.module('myApp').controller('gamesGridController',function(gridDataFactory) {
this.$onInit = function() {
};
this.$postLink = function() {
this.gamesDetails = [];
gridDataFactory.fetchUserDetails().then(function(response) {
this.gamesDetails = response;
console.log(this.gamesDetails);
});
}
});
这是我的工厂:
angular.module('myApp').factory('gridDataFactory', function($http) {
var gameDetails = [];
var obj = {};
obj.fetchUserDetails = function(){
return $http.get('http://starlord.hackerearth.com/gamesarena').then(function(response) {
gameDetails = response.data;
gameDetails.shift();
return gameDetails;
});
}
return obj;
});
正如您所看到的,我的模板中有ng repeat,但它不起作用,并且我无法在视图中看到任何数据
this.$postLink = function() {
this.gamesDetails = [];
gridDataFactory.fetchUserDetails().then(function(response) {
$scope.gamesDetails = response;
console.log(this.gamesDetails);
});
}
在这段代码中,我看到您分配了this.gameDetails,这在范围中不可用。使用$scope.gameDetails将其绑定到视图。您使用的是
此.gamesDetails
,因此将视图更改为使用控制器语法,如下所示
<div class="container" ng-controller="gamesGridController as ctrl">
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Platform</th>
<th>Score</th>
<th>Genre</th>
<th>Editor's Choice</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="detail in ctrl.gamesDetails">
<td>{{detail.title}}</td>
</tr>
</tbody>
</table>
</div>
标题
站台
分数
体裁
编辑的选择
{{detail.title}
在您的gamesGridController
中,您从未调用this.$postLink
函数。也许在它被定义之后直接调用它,以便它在创建控制器时执行。但是我不需要调用$postlink。一旦所有元素都链接到右边,它就会被调用,即使我不使用postlink,只使用$oninit编写代码,它仍然无法工作。但是你能解释一下失败的原因吗。this.$postLink=function(){this.gamesDetails=[];gridDataFactory.fetchUserDetails()。然后(function(response){this.gamesDetails=response;console.log(this.gamesDetails);}
这段代码实际上正确地获取数据,但无法将数据绑定到$scope对象,该对象在控制器的基础视图中可用@NiteshRana看到了吗
<div class="container" ng-controller="gamesGridController as ctrl">
<table class="table table-hover">
<thead>
<tr>
<th>Title</th>
<th>Platform</th>
<th>Score</th>
<th>Genre</th>
<th>Editor's Choice</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="detail in ctrl.gamesDetails">
<td>{{detail.title}}</td>
</tr>
</tbody>
</table>
</div>