此代码段使用AngularJS,jQuery和ASP.NET将数据从服务器移动到客户端。所以在我添加$ .getJSON之前,我只需要一行代码$ scope.cards = // array
当我添加jSON调用时,硬编码的$ scope不会使其查看。我在客户端控制台中也没有看到任何错误。有什么想法吗?
var cardsListController = function ($scope) {
var uri = 'api/products';
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$scope.cards = [{ Id: 1 }, { Id: 2 }]; //**** this works if i move outside this method
});
};
<!DOCTYPE html>
<html ng-app xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/angular.js"></script>
<script src="cardsController.js"></script>
<script src="Scripts/jquery-2.1.1.js"></script>
</head>
<body ng-controller="cardsListController">
<div id="cardsListController">
<div ng-repeat="card in cards">
Card {{card.Id}}
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
你需要像$http service一样使用角度.get()作为ajax,或者使用$scope.$apply()通知angularjs运行其手表
var cardsListController = function ($scope, $https) {
var uri = 'api/products';
//using angularjs
$https.get(uri, {
responseType: 'json'
}).success(function () {
$scope.cards = [{
Id: 1
}, {
Id: 2
}];
});
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$scope.cards = [{
Id: 1
}, {
Id: 2
}]; //**** this works if i move outside this method
//inform angular js to apply its watches so that the view will get updated
$scope.$apply();
});
};