我从这开始:https://github.com/Urigo/meteor-angular-socially/releases/tag/step_06。
有一个Party集合,其中每一方都有名称和描述属性,并且我添加了 thing 数组。数据初始化如下:
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”)。
有人可以解释为什么第三个输入文本没有被重置吗?
答案 0 :(得分: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();
};
});
}]);