我正在尝试使用 AngularJS 的数据绑定(bind)功能来显示我发送 Ajax 请求的数据列表。我是 AngularJS 的新手,所以我知道我在这里遗漏了一些东西。数据正在填充,但仅显示我是否包含带有 ng-model 指令的文本框并开始在其中键入内容。
我知道我的代码并不完全是最佳实践,但我正在尝试在 SharePoint 中执行此操作,但在使用模块和工厂时遇到问题,因此我尝试首先进行简单的演示设置。任何帮助将不胜感激。
代码
<!-- The data will not show until I start typing in this text box
Other than that I do not need it. -->
<input ng-model="foo" />
<ul ng-controller="SimpleController">
<li ng-repeat="item in discussions">
{{item.Title}}
</li>
</ul>
function SimpleController($scope) {
$.ajax({
url: requestUri,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
$scope.discussions = data.d.results;
console.log("SimpleController success callback");
},
error: function (err) {
alert(err);
}
});
}
最佳答案
我相信发生的事情是 Angular 不知道 $.ajax
调用何时完成,因此从技术上讲,您可以使用 if ( $scope.$$phase ) $scope.$apply ()
,但我建议重建 $.ajax
“The Angular Way”:
function SimpleController($scope, $http) {
$http.get( requestUri )
.then(function( json ) {
console.log( json );
$scope.discussions = json.data;
});
}
更多阅读:
-
$scope.$apply
:手动告诉 Angular 在更改 Angular 数据之外的数据时重新计算范围,例如jQuery。 -
$http
:$.ajax
的 Angular 版本,并自动处理$scope.$apply
(推荐)
关于javascript - 直到我开始过滤才显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24372408/