Javascript AngularJS:View不';当$scope中的值更改时,不会更新
我有一个html文件,它的视图从AngularJS范围获取它的值Javascript AngularJS:View不';当$scope中的值更改时,不会更新,javascript,angularjs,Javascript,Angularjs,我有一个html文件,它的视图从AngularJS范围获取它的值 <form ng-controller="EmployeesController as emp" class="signed-in" ng-submit="logOutUser()"> <div class="row"> <div class="col-md-10 text-left"> <h5>Your Games, {{emp.us
<form ng-controller="EmployeesController as emp" class="signed-in" ng-submit="logOutUser()">
<div class="row">
<div class="col-md-10 text-left">
<h5>Your Games, {{emp.username}}</h5>
</div>
<div class="col-md-2">
<button class="btn btn-md btn-primary btn-block" type="submit">Log Out</button>
</div>
</div>
<div class="row store">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Games To Buy</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Game Name</th>
<th>Game Status {{what}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="game in emp.games">
<td>{{game}}</td>
<td>
<button ng-click="buyGame(game.gameId)" type="button" class="btn btn-sm btn-primary btn-block" href="#">Buy</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</form>
}))
嗯。所以基本上问题是,emp.games
变量是空的。首先,它在呈现的HTML页面中得到注释,其次,当我调试页面时,变量emp.games
为空。然而,emp.games应该从$scope.games中获取其值的变量被填充了它应该填充的值。所以问题是我的emp.games
没有看到$scope.games在http请求中被更新,所以它没有更新。我尽可能多地在谷歌上搜索,发现我应该使用$scope.apply,但无论我在哪里使用它,我都会得到错误$digest,它已经在进行中了。。。有什么想法吗
谢谢试试看diggest
if (!$scope.$$phase) {
$scope.$apply(function () {
that.games = data.games;
})
}
else
that.games = data.games;
或者您可以注入$timeout服务并像这样使用它,这样会更好
$timeout (function () {
that.games = data.games;
}, 0)
您的控制器定义无效,请像这样使用
app.controller("EmployeesController", ['$scope','$http','$timeout', function($scope, $http, $timeout){
//....
}]
您尝试将
$scope.$apply()
放在何处?games
是否按照您预期的方式从服务器返回?你通过网络确认了吗?在整个包装的http请求之后,我尝试将其放在http请求的末尾。应该提到的是,我已经尝试了超时(JS'setTimeout)。第一个超时不起作用,但感谢你的回答JS setTimeout无效,请使用角度超时。对于第一个,你把scope标签改成$scope了吗?是的。你能告诉我在这段代码中我在哪里注入$timeout吗,我得到的$timeout没有定义。
app.controller("EmployeesController", ['$scope','$http','$timeout', function($scope, $http, $timeout){
//....
}]