将字符串拆分为多个文本输入 [英] Split a string into multiple text inputs

查看:62
本文介绍了将字符串拆分为多个文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种让用户编辑Vertex 3D字段值的方法.该值存储为字符串,但是我想将其作为三个单独的输入字段显示给用户,以便他们进行编辑.

I need a way for users to edit the value of a field that is Vertex 3D. The value is stored as a string, but I want to display it to the user as three separate input fields for them to edit.

我需要一种用空格分割字符串,然后在单独的输入中显示每个索引的方法.我尝试使用此过滤器,如下所示:

I need a way to split the string by spaces, and then show each index in a separate input. I tried doing with this a filter, like this:

myApp.filter('split', function() {
  return function(input, splitChar, splitIndex) {
    return input.split(splitChar)[splitIndex];
  }
});

<input type="text" ng-model="value | split:' ':0"/>
<input type="text" ng-model="value | split:' ':1"/>
<input type="text" ng-model="value | split:' ':2"/>

但是您无法为过滤器分配值,因此会引发错误.

But you cannot assign a value to a filter so it throws an error.

实现此目标的正确方法是什么?TIA!

What would be the correct way to achieve this? TIA!

推荐答案

我会建议您用空格分隔字符串,并在输入中显示每个部分:

I would recommand to split your string by spaces and show each part in an input:

角度变量

$scope.myString = 'My awesome text';
$scope.arr = $scope.myString.split(/[ ]+/);

HTML输入

<input type="text" ng-model="arr[0]" />
<input type="text" ng-model="arr[1]" />
<input type="text" ng-model="arr[2]" />


在JSFiddle上试用 .

这篇关于将字符串拆分为多个文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆