简体   繁体   English

无法在AngularJS中使用ng-model编辑选择选项

[英]Unable to edit Select Options using ng-model with AngularJS

I have a select that calls the options using ng-model. 我有一个选择,使用ng-model调用选项。 It works perfectly, but now I'm trying to edit the list of options from the array using an input with the model options and it doesn't work. 它可以完美地工作,但是现在我试图使用带有模型选项的输入来编辑数组中的选项列表,但它不起作用。

Long story short, the input with show the options as coma separated values, but once I edit the list, the select will break. 长话短说,输入的内容将选项显示为逗号分隔的值,但是一旦我编辑了列表,选择就会中断。 You can see what I mean in this Plunker . 您可以明白我在这个Plunker中的意思。

The js looks like this: js看起来像这样:

var Select = function($scope) {

$scope.options =
[
    "I live here!",
    "By Boat",
    "By Car",
    "By Plane",
    "By Bike",
    "By Bus",
    "Hitchhiking",
    "Other"
];      

$scope.form = {type : $scope.options[0]}; 
};

And the html: 和html:

 <form class="col-md-12" ng-controller="Select">

 <select class="form-control" ng-model="form.type" ng-options="opt for opt in options"> 
 </select>

 <textarea ng-model="options" class="form-control"></textarea>

 </form>

What am I missing? 我想念什么?

The problem is once you edit the model, it's no longer an array and is stored as a string. 问题是一旦您编辑模型,它就不再是数组,而是存储为字符串。 Add <p> {{ options }} </p> below the text box and you'll see what happens. 在文本框下方添加<p> {{ options }} </p> ,您会看到会发生什么。 What you need to do is to parse the string into an array, and you can do this by writing a method tied to ng-change or a directive. 您需要做的是将字符串解析为数组,您可以通过编写与ng-change或指令绑定的方法来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM