最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In Angular, how do I efficiently split input items into an array - Stack Overflow

programmeradmin1浏览0评论

When binding input value to an ng-model like so:

<input type="text" ng-model="array">

how do I bind the input text as an array? So if I input one, two, three, the resulting model will be [ "one","two","three ].

Right now this is how I'm achieving this:

<input type="text" ng-model="string" ng-change="convertToArray()">

And in my controller:

$scope.convertToArray = function(){
    $scope.array = $scope.string.split(',');
}

It works fine but I don't think it's best practice because I'm making a $scope.string variable and then hardcoding the destination array.

Is it possible to just have the input's model set into array and then have the input pass through the function before being bound to the scope?

When binding input value to an ng-model like so:

<input type="text" ng-model="array">

how do I bind the input text as an array? So if I input one, two, three, the resulting model will be [ "one","two","three ].

Right now this is how I'm achieving this:

<input type="text" ng-model="string" ng-change="convertToArray()">

And in my controller:

$scope.convertToArray = function(){
    $scope.array = $scope.string.split(',');
}

It works fine but I don't think it's best practice because I'm making a $scope.string variable and then hardcoding the destination array.

Is it possible to just have the input's model set into array and then have the input pass through the function before being bound to the scope?

Share Improve this question edited Jan 9, 2013 at 10:56 ЯegDwight 25.2k10 gold badges47 silver badges52 bronze badges asked Jan 9, 2013 at 10:55 George Ananda EmanGeorge Ananda Eman 3,3528 gold badges30 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 20

ngList will do exactly what you want.

Text input that converts between comma-separated string into an array of strings.

No. Since your input will be a string from the user, you need to manually convert it to array or any other format.

Similar discussion and other approach is discussed here. probably this might help. (see accepted answer in this)

How do I set the value property in AngularJS' ng-options?

发布评论

评论列表(0)

  1. 暂无评论