2015-09-27 72 views
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(); 
     }; 
    }]); 

視圖(黨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個輸入並單擊Rese t形成按鈕,然後每個輸入將恢復爲除第三個以外的原始文本(使用ng-model =「aThing.thing」)。

有人可以解釋爲什麼第三個輸入文本不會被重置嗎?

回答

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

}]);