2015-09-11 37 views
0

我的原始錯誤是我的選擇選項沒有填充,我發現這是因爲角度看不到我的作用域,因爲它在我的「創建」函數中。當我將範圍對象移出該功能時,它填充了我的選項,但是當我選擇了一個選項時,它們消失了...... 這是因爲我的選擇被綁定到了無法訪問該範圍的ng模型中。如果我改變模型,當我將它保存到數據庫時,它將它保存爲[object Object],[object Object],[object Object] 這不是我想要的。在使用ng-options顯示和保存時遇到問題

我很困惑,因爲我的選擇不需要綁定到正確的模型,以保存到數據庫中的正確屬性? 當你不應該在模型中操縱日期時,模型如何看見範圍?

任何幫助,將不勝感激

模型

var StrainSchema = new Schema({ 
 
\t name: { 
 
\t \t type: String, 
 
\t \t default: '', 
 
\t \t required: 'Please specify a Strain name', 
 
\t \t trim: true 
 
\t }, 
 
\t description: { 
 
\t \t type: String, 
 
\t \t default: '', 
 
\t \t required: 'Describe the Strain', 
 
\t \t trim: true 
 
\t }, 
 
\t stype: { 
 
\t \t type: String, 
 
\t \t default: '', 
 
\t \t required: 'Specify type of Strain', 
 
\t \t trim: true 
 
\t }, 
 
\t created: { 
 
\t \t type: Date, 
 
\t \t default: Date.now 
 
\t }, 
 
\t user: { 
 
\t \t type: Schema.ObjectId, 
 
\t \t ref: 'User' 
 
\t } 
 
});

控制器

$scope.stype = [ 
 
\t \t \t \t {id: '1', name: 'Indica'}, 
 
\t \t \t \t {id: '2', name: 'Sativa'}, 
 
\t \t \t \t {id: '3', name: 'Hybrid'} 
 
\t \t \t ]; 
 

 
\t \t // Create new Strain 
 
\t \t $scope.create = function() { 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t // Create new Strain object 
 
\t \t \t var strain = new Strains ({ 
 
\t \t \t \t name: this.name, 
 
\t \t \t \t description: this.description, 
 
\t \t \t \t stype: this.stype 
 
\t \t \t }); 
 
\t \t \t 
 
\t \t \t 
 

 
\t \t \t // Redirect after save 
 
\t \t \t strain.$save(function(response) { 
 
\t \t \t \t $location.path('strains/' + response._id); 
 

 
\t \t \t \t // Clear form fields 
 
\t \t \t \t $scope.name = ''; 
 
\t \t \t \t $scope.description = ''; 
 
\t \t \t \t $scope.stype = ''; 
 
\t \t \t }, function(errorResponse) { 
 
\t \t \t \t $scope.error = errorResponse.data.message; 
 
\t \t \t }); 
 
\t \t };

HTML/VIEW

<label class="control-label" for="stype">Type</label> 
 
        <div class="controls"> 
 
         <select name="stype" id="stype" class="form-control" data-ng-model="stype" data-ng-options="type.name for type in stype"> 
 
          <option value="">-- Choose Type --</option> 
 
         </select> 
 
        </div>

+1

也許是因爲你在「ng-model」和「ng-options」中使用了相同的變量名(stype)。一旦你選擇了一個選項,「stype」就變成了一個字符串,而不再是一個數組。嘗試爲model和你的stype數組使用不同的名字(例如stypes)。 – nersoh

+0

這就是問題所在,謝謝! 現在我得到一個新的錯誤....我得到了「指定類型的應變」,它看起來像它沒有在我的模型中傳遞所需的參數。 任何想法爲什麼? –

+1

你可以做一個蹲式嗎? – macrog

回答

0

問題是,我不是指定的值屬性正確的字符串。

data-ng-options="type.name as type.name for type in stypes"

第一type.name把我的「名」串入值屬性的值是什麼被保存到數據庫中,而不是在選項的文本。

相關問題