[英]Having trouble displaying and saving with ng-options
我最初的错误是我的选择选项没有填充,我发现这是因为angular看不到我的范围,因为它在我的“创建”函数中。 当我将范围对象移出该函数时,它会填充我的选项,但是当我选择一个选项时,它们就会消失...。这是因为我的选择被绑定到了无法访问该范围的ng模型中。 如果更改模型,则在将其保存到数据库时会将其另存为[object Object],[object Object],[object Object],这不是我想要的。
我很困惑,是因为我的选择不需要绑定到正确的模型上才能保存到数据库中的正确属性吗? 当您不应该在模型中操纵日期时,模型如何看到范围?
任何帮助将不胜感激
模型
var StrainSchema = new Schema({ name: { type: String, default: '', required: 'Please specify a Strain name', trim: true }, description: { type: String, default: '', required: 'Describe the Strain', trim: true }, stype: { type: String, default: '', required: 'Specify type of Strain', trim: true }, created: { type: Date, default: Date.now }, user: { type: Schema.ObjectId, ref: 'User' } });
控制器
$scope.stype = [ {id: '1', name: 'Indica'}, {id: '2', name: 'Sativa'}, {id: '3', name: 'Hybrid'} ]; // Create new Strain $scope.create = function() { // Create new Strain object var strain = new Strains ({ name: this.name, description: this.description, stype: this.stype }); // Redirect after save strain.$save(function(response) { $location.path('strains/' + response._id); // Clear form fields $scope.name = ''; $scope.description = ''; $scope.stype = ''; }, function(errorResponse) { $scope.error = errorResponse.data.message; }); };
HTML /查看
<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>
问题是我没有为value属性指定正确的字符串。
data-ng-options="type.name as type.name for type in stypes"
第一个type.name将我的“名称”字符串放入value属性,该值是保存到数据库中的内容,而不是选项中的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.