范围变量在 ajax 发布请求后未更改


Scope variable not changing after ajax post request

我正在做一个Yahtzee SPA。这是我的js文件

var game = angular.module('yahtzee',[]);
game.controller('RollDicesController', function($scope, $http) {
  $scope.img1 = "style/img/dice1.jpg";
  $scope.img2 = "style/img/dice2.jpg";
  $scope.img3 = "style/img/dice3.jpg";
  $scope.img4 = "style/img/dice4.jpg";
  $scope.img5 = "style/img/dice5.jpg"; 
 $scope.result = {
     Ones : '0',
     Twos : '0',
     Threes : '0',
     Fours : '0',
     Fives : '0',
     Sixes : '0',
     ThreeOfAKind : '0',
     FourOfAKind : '0',
     FullHouse : '0',
     SmallStraight : '0',
     LargeStraight : '0',
     Chance: '0',
     Yahtzee : '0'
 };
 $scope.notSorted = function(obj){
     if (!obj) {
         return [];
     }
     return Object.keys(obj);
 }
 $scope.rollDices = function() {
     $dice1 = Math.floor((Math.random() * 6) + 1);
     $dice2 = Math.floor((Math.random() * 6) + 1);
     $dice3 = Math.floor((Math.random() * 6) + 1);
     $dice4 = Math.floor((Math.random() * 6) + 1);
     $dice5 = Math.floor((Math.random() * 6) + 1);
     $scope.img1 = "style/img/dice" + $dice1 + ".jpg";
     $scope.img2 = "style/img/dice" + $dice2 + ".jpg";
     $scope.img3 = "style/img/dice" + $dice3 + ".jpg";
     $scope.img4 = "style/img/dice" + $dice4 + ".jpg";
     $scope.img5 = "style/img/dice" + $dice5 + ".jpg";
     $http.post('calculate', {'dice1': $dice1, 'dice2': $dice2, 'dice3': $dice3, 'dice4': $dice4, 'dice5': $dice5 }).success(function(data) {
         $scope.result = data;
     }).error(function(data, status) { 
         $scope.errors.push(status);
     });
  };
});

请求所做的几乎是相应移动的属性值。我有一个连接到calculate路由的php控制器(我正在使用Laravel),它完成所有的数学工作。现在的问题是,尽管我正确接收了值(我已经对此进行了测试),但我的表没有更新。

我的网页中已经有一个定义了控制器的主div:

<div ng-controller="RollDicesController">
    <div id="game_div">
        <div id="dice_div">
            <ul>
              <li style="list-style-type: none;" class="listaDados"  >
               <img id="dice1" ng-src="{{img1}}">
               <img id="dice2" ng-src="{{img2}}" > 
               <img id="dice3" ng-src="{{img3}}" > 
               <img id="dice4" ng-src="{{img4}}" >
               <img id="dice5" ng-src="{{img5}}" > 
              </li>
            </ul>
            <a>  
                <button id="rolldice_button" class= "button_class" ng-click="rollDices()">
                    Roll Dice!
                </button>
            </a>  
    <div id="table_div">
        <table id="score_table" border="1" >
            <tr ng-repeat="key in notSorted(result) track by $index" ng-init="val = result[key]">
                <td> {{ key }} </td>
                <td> {{ val }} </td>
            </tr>
        </table>
    </div>
</div>

出于某种原因,桌子永远不会改变,尽管$scope.result和骰子图像会改变。

真的不明白我做错了什么,所以我需要你的帮助。

编辑1:我发现删除这个后:

     $scope.result = {
     Ones : '0',
     Twos : '0',
     Threes : '0',
     Fours : '0',
     Fives : '0',
     Sixes : '0',
     ThreeOfAKind : '0',
     FourOfAKind : '0',
     FullHouse : '0',
     SmallStraight : '0',
     LargeStraight : '0',
     Chance: '0',
     Yahtzee : '0'
 };

我按下按钮,它工作了一次,接下来的点击并没有改变桌子。出于某种原因,在我给$scope.result一个值后,它的值再也不会改变。由于我对AngularJS的了解有限,我不知道为什么会发生这种情况。

编辑2:我尝试使用$scope.apply()更改请求,如下所示

$http.post('calculate', {'dice1': $dice1, 'dice2': $dice2, 'dice3': $dice3, 'dice4': $dice4, 'dice5': $dice5 }).success(function(data) {
            $scope.result = data;
            $scope.apply();
    }).error(function(data, status) { 
        $scope.errors.push(status);
    });

编辑3:$scope.$apply在我之前的编辑中写得不好,更正后,现在向我显示此错误

Error: error:inprog Action Already In Progress $digest already in progress

最终编辑:$timeout有效,问题是使用 notSort 函数。

现在,该表的创建方式如下:

        <div id="table_div">
        <table id="score_table" border="1" >
            <tr ng-repeat="(key, val) in result track by $index">
                <td> {{ key }} </td>
                <td> {{ val }} </td>
            </tr>
        </table>
    </div>

虽然它没有按照我想要的方式排序,但至少它可以工作。

试试这个,建议不要使用$apply。

注意我在顶部注入$timeout。

var game = angular.module('yahtzee',[]);
game.controller('RollDicesController', function($scope, $http, $timeout) {
  $scope.img1 = "style/img/dice1.jpg";
  $scope.img2 = "style/img/dice2.jpg";
  $scope.img3 = "style/img/dice3.jpg";
  $scope.img4 = "style/img/dice4.jpg";
  $scope.img5 = "style/img/dice5.jpg"; 
 $scope.result = {
     Ones : '0',
     Twos : '0',
     Threes : '0',
     Fours : '0',
     Fives : '0',
     Sixes : '0',
     ThreeOfAKind : '0',
     FourOfAKind : '0',
     FullHouse : '0',
     SmallStraight : '0',
     LargeStraight : '0',
     Chance: '0',
     Yahtzee : '0'
 };
 $scope.notSorted = function(obj){
     if (!obj) {
         return [];
     }
     return Object.keys(obj);
 }
 $scope.rollDices = function() {
     $dice1 = Math.floor((Math.random() * 6) + 1);
     $dice2 = Math.floor((Math.random() * 6) + 1);
     $dice3 = Math.floor((Math.random() * 6) + 1);
     $dice4 = Math.floor((Math.random() * 6) + 1);
     $dice5 = Math.floor((Math.random() * 6) + 1);
     $scope.img1 = "style/img/dice" + $dice1 + ".jpg";
     $scope.img2 = "style/img/dice" + $dice2 + ".jpg";
     $scope.img3 = "style/img/dice" + $dice3 + ".jpg";
     $scope.img4 = "style/img/dice" + $dice4 + ".jpg";
     $scope.img5 = "style/img/dice" + $dice5 + ".jpg";
     $http.post('calculate', {'dice1': $dice1, 'dice2': $dice2, 'dice3': $dice3, 'dice4': $dice4, 'dice5': $dice5 }).success(function(data) {
         $timeout(function() {
             $scope.result = data;
         });
     }).error(function(data, status) { 
         $scope.errors.push(status);
     });
  };
});

尝试以下代码:

<div id="table_div">
        <table id="score_table" border="1" >
            <tr ng-repeat="key in result track by $index" ng-init="val = result[key]">
                <td> {{ key }} </td>
                <td> {{ val }} </td>
            </tr>
        </table>
</div>

或者试试这个:

$http.post('calculate', {'dice1': $dice1, 'dice2': $dice2, 'dice3': $dice3, 'dice4': $dice4, 'dice5': $dice5 }).success(function(data) {
         $timeout(function() {
             $scope.result = data;
         });
     }).error(function(data, status) { 
         $scope.errors.push(status);
     });
$timeout to avoid $digest error.