【问题标题】:Unable to edit Select Options using ng-model with AngularJS无法使用 ng-model 和 AngularJS 编辑选择选项
【发布时间】:2014-03-03 02:42:14
【问题描述】:

我有一个使用 ng-model 调用选项的选择。它工作得很好,但现在我正在尝试使用带有模型选项的输入来编辑数组中的选项列表,但它不起作用。

长话短说,输入将选项显示为逗号分隔值,但是一旦我编辑列表,选择就会中断。您可以在Plunker 中看到我的意思。

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]}; 
};

还有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>

我错过了什么?

【问题讨论】:

标签: javascript angularjs angular-ngmodel


【解决方案1】:

问题是一旦您编辑模型,它就不再是一个数组,而是存储为一个字符串。 在文本框下方添加&lt;p&gt; {{ options }} &lt;/p&gt;,您将看到会发生什么。您需要做的是将字符串解析为数组,您可以通过编写与 ng-change 或指令绑定的方法来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2015-05-06
    相关资源
    最近更新 更多