Angular - 无法观察工厂变量

时间:2016-02-03 22:46:01

标签: javascript angularjs variables angularjs-scope watch

我一直试图让控制器的范围识别工厂变量。

这是我的index.html

<div ng-controller='scoresController'>
    <div id='game-info' ng-hide='!playing'>
        <p id='level'>Level: {{ level }}
            <span id='score'>Score: {{ score }}</span>
            <span id='high-score'>High Score: {{ highScore() }}</span>
            <span id='coins'>Coins: <span class='coins-badge'> {{ coins }} </span></span>
        </p>
    </div>
</div>

这是我的工厂

angular.module('my-app')
    .factory('Game', function() {
        return { 
            level: 1,
            score: 0,
            playing: false,
            coins: localStorage['snakeCoins']
        };
    });

这是我的控制器$watch函数

angular.module('my-app')
.controller('scoresController', ['$scope', 'Game', function($scope, Game) {

    $scope.score = Game.score;
    $scope.level = Game.level;
    $scope.playing = false;
    //$scope.coins = localStorage['snakeCoins'];

    // Why aren't the changes being registered?
    $scope.$watch(function() {
        return Game.playing;
        }, function(newVal, oldVal) {
        $scope.playing = newVal;
    }, true);

    $scope.$watch(function() {
        return Game.score;
        }, function(newScore, oldScore) {
        if (newScore > oldScore) {
            console.log(newScore);
            $scope.score = newScore;
        }
    });

    $scope.$watch(function() {
        return Game.level;
        }, function(newLevel, oldLevel) {
        if (newLevel > oldLevel) {
            $scope.level = newLevel;
        }
    });

但是,我知道工厂变量正在发生变化,因为我将结果记录到控制台

$scope.incrementScore = function() {
        if (!playing) {
            if (apple['x'] == snake[0].x && apple['y'] == snake[0].y) {
                Game.score++;
                console.log(Game.score);      //Logging here
                $scope.incrementLevel();
                $scope.generateApples();
                $scope.drawApple(apple['x'], apple['y']);
                snake.push({ x: snake.length, y: 0 });

            }
        }
    }

它不是包含文件的订单。

<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js'></script>
<script src='scripts/data.js'></script>
<script src='scripts/app.js'></script>
<script src='scripts/services/game.js'></script>
<script src='scripts/controllers/navController.js'></script>
<script src='scripts/controllers/achievementsController.js'></script>
<script src='scripts/controllers/scoresController.js'></script>
<script src='scripts/controllers/gamesController.js'></script>

不确定,为什么我无法通过$watch注意到这些更改。任何理论?

2 个答案:

答案 0 :(得分:2)

您应该使用$rootScope.$watch

答案 1 :(得分:1)

您可以通过将Game分配给范围来简化此操作,即

.controller('scoresController', function($scope, Game) {
    $scope.game = Game;

并在您的模板中......

<span id="score">Score: {{ game.score }}</span>

这样,您根本不需要设置任何手动观察者。