Angularjs Can';我不知道如何使用$scope。$watch

Angularjs Can';我不知道如何使用$scope。$watch,angularjs,angularjs-scope,Angularjs,Angularjs Scope,如果有应用程序控制器。在这个控制器中,我调用一个名为“saveGame”的资源服务。saveGame有一个名为“resolved”的属性。我用false初始化这个属性。当承诺解析(保存游戏已加载)时,我将resolved属性设置为true 以上部分工作 现在,我将saveGame服务注入MainCtrl。 如果我在本地运行代码,它就会工作,因为saveGame几乎可以立即解析。但是如果我打开延迟-模仿一个非常慢的连接。saveGame无法及时解决,我需要等待它 这是我的密码: applicati

如果有应用程序控制器。在这个控制器中,我调用一个名为“saveGame”的资源服务。saveGame有一个名为“resolved”的属性。我用false初始化这个属性。当承诺解析(保存游戏已加载)时,我将resolved属性设置为true

以上部分工作

现在,我将saveGame服务注入MainCtrl。 如果我在本地运行代码,它就会工作,因为saveGame几乎可以立即解析。但是如果我打开延迟-模仿一个非常慢的连接。saveGame无法及时解决,我需要等待它

这是我的密码:

application
    .controller('MainCtrl',
        ['$scope', 'saveGame', function ($scope, saveGame) {
            'use strict';

            // At this point the savegame should be resolved
            // If not we need to wait for it.
            $scope.$watch(saveGame.resolved, function ( newVal, oldVal) {
                console.log( newVal + '-' + oldVal ); // #1
                if(newVal !== undefined ){
                    //do your stuff here...
                    console.log("do your stuff"); // #2
                }
            });
因此,1的newVal和oldVal是未定义的。 #2处的控制台日志从未触发

为完整起见,我的saveGame服务如下所示:

application
    .factory('saveGame',
        ['$resource', 'config', function ($resource, config) {
            'use strict';

            var _resolved = false;
            var _location = {};

            var _load = function (playerId) {
                var res;
                if (config.getSetting('use-mockup')) {
                    // Use mockup data
                    // $resource returns a class representation
                    res = $resource('data/mockup/savegame/player:id.json');
                } else {
                    // TODO: Use real API
                }
                // This will return a promise!
                return res.get({id: playerId}).$promise;
            };

            var _save = function (stuff) {
                return stuff;
            };

            return {
                load: _load,
                save: _save,
                location: _location,
                resolved: _resolved
            };
        }]);
// Load the savegame resource
            var promise = saveGame.load(1); // PlayerId
            promise.then(function (resource) {
                // savegame resource was loaded and resolved
                saveGame.location = resource.location;
                saveGame.resolved = true;
            });
我的appCtrl中的部分如下所示:

application
    .factory('saveGame',
        ['$resource', 'config', function ($resource, config) {
            'use strict';

            var _resolved = false;
            var _location = {};

            var _load = function (playerId) {
                var res;
                if (config.getSetting('use-mockup')) {
                    // Use mockup data
                    // $resource returns a class representation
                    res = $resource('data/mockup/savegame/player:id.json');
                } else {
                    // TODO: Use real API
                }
                // This will return a promise!
                return res.get({id: playerId}).$promise;
            };

            var _save = function (stuff) {
                return stuff;
            };

            return {
                load: _load,
                save: _save,
                location: _location,
                resolved: _resolved
            };
        }]);
// Load the savegame resource
            var promise = saveGame.load(1); // PlayerId
            promise.then(function (resource) {
                // savegame resource was loaded and resolved
                saveGame.location = resource.location;
                saveGame.resolved = true;
            });
所以我知道我做错了什么,但我就是不知道是什么? 同样的帮助也将不胜感激。

您需要:

        $scope.$watch(function () { return saveGame.resolved; }, function ( newVal, oldVal) {
            console.log( newVal + '-' + oldVal ); // #1
            if(newVal !== undefined ){
                //do your stuff here...
                console.log("do your stuff"); // #2
            }
        });
您必须使用字符串作为watch或function的第一个参数,并返回值。如果使用字符串“a.b.c”,AngularJS comparing将在当前和下一个周期中比较
$scope.a.b.c
。若在那个里使用函数,AngularJS将比较函数的结果

这里更好的方法可能是更新
saveGame
服务并在其中回报承诺。在任何需要saveGame结果的地方,您都可以使用以下内容:

module.factory('saveService', ['$http', function ($http) {
    return {
        doSave: function () {
            return $http.get(params);
        }
    }
}])

module.controller('MainCtrl', ['saveService', function (saveService) {
    saveService.doSave().then(function onSuccess() {
        // handle something after save
    });
}])

您如何在saveGame服务中获取数据?也许本质上是异步的?我用saveGame服务代码和初始化它的应用程序控制器部分更新了我的问题@您确定可以在控制器中查看服务变量吗?把saveGame.resolved分配到控制器的scope变量上并在上面放一块手表怎么样?哇。那起作用了。。。您的代码中有一个小错误:{return saveGame.resolved;})与saveGame服务一样。给小费。我会重写它,在内部解决它。那样更干净!