简体   繁体   English

AngularJS:当$ scope中的值更改时,View不会更新

[英]AngularJS : View doesn't update when a value in $scope changes

I have an html file with a view that gets it's values from an AngularJS scope. 我有一个带有视图的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.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>

.

And the js file : 和js文件:

var app = angular.module("MyApp", ['ngRoute']);
app.controller("EmployeesController", function($scope, $http) {

this.games = [{gameName: 'aaa', gameId: '1'},{gameName: 'aaa', gameId: '1'},{gameName: 'aaa', gameId: '1'}];
this.ownedGames = [];

var that = this;

$scope.sellGame = function(gameId) {
    var index = that.ownedGames.indexOf(gameId);
    that.ownedGames.splice(index, 1);
    $.jStorage.set(that.username, that.ownedGames);
}

$scope.$on('$viewContentLoaded', function() {
    if($.jStorage.get('Employee') == null)
        $scope.logged = false;
    else
        $scope.logged = true;

    $http.get('employees.json').
    success(function(data, status, headers, config) {
            that.games = data.games;
            that.username = $.jStorage.get('Employee');
            if($.jStorage.get(that.username) != null)
                that.ownedGames = $.jStorage.get(that.username);
    });
});

}); });

Ok. 好。 So basically what the problem is, is that the emp.games variable is empty. 基本上问题是, emp.games变量是空的。 First of all it gets commented in the rendered HTML page, and second when i debug the page the variable emp.games is empty. 首先,它在呈现的HTML页面中被注释,第二,当我调试页面时,变量emp.games为空。 Yet the variable that emp.games is supposed to get its values from, the $scope.games is filled with values as it should be. 然而,emp.games应该从其获取其值的变量,$ scope.games充满了应有的值。 So the problem is that my emp.games doesn't see that $scope.games was updated in that http request, so IT doesn't update. 所以问题是我的emp.games没有看到$ scope.games在该http请求中被更新,因此IT不会更新。 I googled as much as i could and found out that i should use $scope.apply, but wherever i used it i got the error $digest already in progress... any ideas? 我尽可能多地用Google搜索,发现我应该使用$ scope.apply,但无论我在哪里使用它,我都会收到错误$ digest正在进行中......有什么想法吗?

Thanks 谢谢

Try check diggest 尝试检查diggest

if (!$scope.$$phase) {
     $scope.$apply(function () {
           that.games = data.games;
       })
     }
else
   that.games = data.games;

Or you can inject $timeout service and use it like this, it will be better. 或者你可以注入$ timeout服务并像这样使用它,它会更好。

$timeout (function () {
           that.games = data.games;
 }, 0)

Your controller definition is not valid, please use like this. 您的控制器定义无效,请使用这样的。

 app.controller("EmployeesController", ['$scope','$http','$timeout', function($scope, $http, $timeout){
  //....
 }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM