Javascript 不要打印任何东西
我有以下情况:Javascript 不要打印任何东西,javascript,jquery,angularjs,angularjs-ng-repeat,Javascript,Jquery,Angularjs,Angularjs Ng Repeat,我有以下情况: app.controller('ResourceController', function($scope, $sce){ var resourceData; $scope.data = ''; $scope.loadResources = function(){ $.get('controllers/getResources.php', function(result){ co
app.controller('ResourceController', function($scope, $sce){
var resourceData;
$scope.data = '';
$scope.loadResources = function(){
$.get('controllers/getResources.php', function(result){
console.log(result);
resourceData = $.parseJSON(result);
$scope.data = $sce.parseAsHtml(resourceData);
});
};
$scope.loadResources();
});
上面的控制器向服务器发出请求,以便它可以加载所有资源并将它们发布到html表中
以下是控制台日志(结果)的结果:
这是一个大的JSON,数据就在那里,很好
在HTML中,我正在做的是:
<div class="container-fluid" ng-controller="ResourceController as resource">
.....
<tbody>
<tr ng-repeat="resourceInfo in data">
<td>{{resourceInfo.ID}}</td>
<td>{{resourceInfo.NAME}}</td>
<td>{{resourceInfo.DESCRIPTION}}</td>
<td>{{resourceInfo.STATUS}}</td>
<td>{{resourceInfo.STATUS_DESCRIPTION}}</td>
<td>{{resourceInfo.LOCATION}}</td>
<td>
<a class="btn btn-danger" data-toggle="modal" data-target="#delete-prompt">Delete</a>
<button class="btn btn-info" data-toggle="modal" data-target="#update-form">Update</button>
</td>
</tr>
</tbody>
console.log($scope.data[0].ID)
打印'123'
。我不明白这一点,我以为这是JSON格式的,因为我确实将其解析为JSON,但当我尝试在HTML上打印它时,它就是无法打印 问题:
这可能是因为您使用jQuery而不是Angular加载数据。由于数据是异步加载的,Angular不知道它们在那里,因此它不支持视图
解决:
在您的情况下,
$http
服务和Angular的fromJson()
方法(Angular.fromJson(…)
)应该完成这项工作$.get('controllers/getResources.php', function (result) {
$scope.$apply(function () {
...
$scope.data = $.parseJSON(result);
});
});
问题似乎是
$scope.data
不是数据收集,而是一个函数。请参阅控制台日志判断,$scope.data已转换为数组。尝试将其定义为第一个索引(假设所有可能的输出都是这样的。$scope.data不是数组,而是JSON。@ArunPJohny我正试图做和你在那里做的一样的事情,但它仍然没有打印出来。老兄,你真是个救命恩人。我不知道我不应该使用propper jQuery ajax请求,而应该使用angular。所以说清楚一点,你能解释一下吗We我怎么能对angular的$http
做同样的事情呢?angular给我的唯一(糟糕)经历是在codeschool上的课程。一门不太好的begginers课程。他们没有解释我刚才在你的答案上读到的任何内容。因为angular还为你将JSON响应解析为对象(默认情况下,您当然可以更改它),您可能只需要注入$http
,并像这样使用它:$http.get('controllers/getResources.php')。成功(函数(data){$scope.data=data;})。错误(函数(拒绝){/*处理请求错误…*/})
有趣的是,我将更改它并尝试一下。还有一个问题。angular是否严重依赖承诺?我对延迟对象和处理承诺非常陌生。@hsoto,是的,angular严重使用承诺。angular不依赖承诺。它支持并使用它,但问题是你的应用程序在多大程度上依赖承诺。承诺是Angular处理异步操作的方法。如果你想支持异步操作,你必须使用Promission。
app.controller('ResourceController', function($scope, $sce){
$scope.data = '';
$scope.loadResources = function(){
$.get('controllers/getResources.php', function(result){
console.log(result);
$scope.data = $.parseJSON(result);
console.log($scope.data);
console.log($scope.data[0].ID);
});
};
$scope.loadResources();
});
$.get('controllers/getResources.php', function (result) {
$scope.$apply(function () {
...
$scope.data = $.parseJSON(result);
});
});