【问题标题】:$meteor.object reset function changes not reflected in user interface$meteor.object 重置功能更改未反映在用户界面中
【发布时间】:2015-09-27 17:33:29
【问题描述】:

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

有一个Parties 集合,其中每一方都有name 和description 属性,我添加了一个things 数组。数据初始化如下:

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")。

谁能解释为什么第三个输入文本没有被重置?

【问题讨论】:

    标签: angularjs meteor angular-meteor


    【解决方案1】:

    $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();
            };
        });
    
    }]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多