0

我从这个开始:https ://github.com/Urigo/meteor-angular-socially/releases/tag/step_06 。

有一个缔约方集合,其中每个缔约方都有名称和描述属性,并且我添加了一个事物数组。数据初始化如下:

if (Meteor.isServer) {
    Meteor.startup(function () {
        if (Parties.find().count() === 0) {
            var parties = [
                {
                    'name': 'Dubstep-Free Zone',
                    'description': 'Fast just got faster with Nexus S.',
                    'things': [{'thing': 'Thing 1'},{'thing': 'Thing 2'}]
                },
                {
                    'name': 'All dubstep all the time',
                    'description': 'Get it on!',
                    'things': [{'thing': 'Thing 1'},{'thing': 'Thing 2'}]
                },
                {
                    'name': 'Savage lounging',
                    'description': 'Leisure suit required. And only fiercest manners.',
                    'things': [{'thing': 'Thing 1'},{'thing': 'Thing 2'}]
                }
            ];
            for (var i = 0; i < parties.length; i++)
                Parties.insert(parties[i]);
        }
    });
}

这是控制器定义:

    angular.module("socially").controller("PartyDetailsCtrl", ['$scope', '$stateParams', '$meteor',
    function ($scope, $stateParams, $meteor) {
        $scope.party = $meteor.object(Parties, $stateParams.partyId, false);
        $scope.aThing = $scope.getReactively('party.things[0]');
        $scope.favoriteThingIndex = 1;

        $scope.save = function () {
            $scope.party.save().then(function (numberOfDocs) {
                console.log('save success doc affected ', numberOfDocs);
            }, function (error) {
                console.log('save error', error);
            });
        };

        $scope.reset = function () {
            $scope.party.reset();
        };
    }]);

视图 (party-details.ng.html) 如下所示:

Here you will see and change the details of the party:

<input ng-model="party.name">
<input ng-model="party.description">
<input ng-model="aThing.thing">
<input ng-model="party.things[favoriteThingIndex].thing">

<button ng-click="save()">Save</button>

<button ng-click="reset()">Reset form</button>
<button ui-sref="parties">Cancel</button>

我可以更改 4 个输入中的任何一个并单击“保存”按钮,更改将成功保存,但如果我更改所有 4 个输入并单击“重置表单”按钮,则每个输入将恢复为原始文本,除了第三个(使用 ng -model="aThing.thing")。

有人可以解释为什么第三个输入文本没有被重置吗?

4

1 回答 1

0

$scope.getReactively需要包裹在$meteor.autorun

function ($scope, $stateParams, $meteor) {
    $meteor.autorun($scope, function() {
        $scope.party = $meteor.object(Parties, $stateParams.partyId, false);
        $scope.aThing = $scope.getReactively('party.things[0]');
        $scope.favoriteThingIndex = 1;

        $scope.save = function () {
            $scope.party.save().then(function (numberOfDocs) {
                console.log('save success doc affected ', numberOfDocs);
            }, function (error) {
                console.log('save error', error);
            });
        };

        $scope.reset = function () {
            $scope.party.reset();
        };
    });

}]);
于 2015-10-11T00:40:33.360 回答