简体   繁体   English

Angular.js:使用TextArea中的新行打印数组模型

[英]Angularjs: Printing an Array model with new lines in a TextArea

I have a simple textarea control, 我有一个简单的textarea控件,

<div ng-controller="TodoCtrl">
      <textarea ng-model="myArray"></textarea>
  </div>

and a model that is an array of strings: 以及一个由字符串数组组成的模型:

function TodoCtrl($scope) {
  $scope.myArray = [
      "something1", 
      "something2",
      "something3",
   ];    
}

Im trying to get it so that every entry is on its own line: 我试图让它,以便每个条目都在自己的行上:

something1 
something2
something3

And for extra credit would like to resplit the text back into an array on hte model side. 为了获得额外的荣誉,我们希望将文本重新拆分为模型一侧的数组。

I've tried a few things, including ng-list , custom directives , custom filters, and ng-repeat, but haven't gotten any where. 我已经尝试了一些方法,包括ng-list自定义指令 ,自定义过滤器和ng-repeat,但是没有任何地方。

fiddle Example: 小提琴例子:

http://jsfiddle.net/U3pVM/8162/ http://jsfiddle.net/U3pVM/8162/

What you want is to have two vars. 您想要的是拥有两个变量。 Essentially your textarea is a text string. 本质上,您的textarea是文本字符串。 So if you need an array then set up a watcher on the string model. 因此,如果您需要一个数组,请在字符串模型上设置观察者。

http://jsfiddle.net/U3pVM/8165/ http://jsfiddle.net/U3pVM/8165/

$scope.myStr = 'aaaa\nbbbb\nccccc';
$scope.$watch('myStr', function(myStr){
    $scope.myArray = myStr.split('\n');
    console.log('$scope.myArray', $scope.myArray);
});

I'm not sure I understand what you're trying to achieve. 我不确定我了解您要达到的目标。 There is no textarea in the fiddle. 小提琴中没有文本区域。

You can doctor your array to have new line characters at the end of each string. 您可以修改数组,使每个字符串的末尾都有换行符。 Not sure if that helps? 不确定是否有帮助?

$scope.myArr = ['111\n', '222\n', '333\n']

http://jsfiddle.net/U3pVM/8161/ http://jsfiddle.net/U3pVM/8161/

Your fiddle has quite a lot of info in, maybe make it simpler? 您的小提琴有很多信息,也许使它更简单? Showing input and output. 显示输入和输出。

Edit: 编辑:

Is it for input and output? 用于输入和输出吗? If just for output, you could just do something like this: http://jsfiddle.net/U3pVM/8164/ 如果仅用于输出,则可以执行以下操作: http : //jsfiddle.net/U3pVM/8164/

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

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