简体   繁体   English

如何使我的textarea最大长度与ng-model确定的当前长度相对应?

[英]How can I make my textarea maxlength correspond to its current length as determined by ng-model?

I have a TextArea , and underneath is a character count. 我有一个TextArea ,下面是一个字符计数。 The html is the following: html如下:

<textarea class="form-control rows="7" ng-model="feedback" maxlength="1000"></textarea>
<span>{{1000 - feedback.length}}</span>

The controllers are all wired up properly and so forth, but the issue is with how the two counters are counting. 控制器均已正确连接,依此类推,但问题在于两个计数器的计数方式。 The max length attribute (as well as the ng-maxlength, for that matter), will include carriage returns. 最大长度属性(以及ng-maxlength)将包括回车符。 However, feedback is just a string in my scope, which excludes any carriage returns. 但是, feedback只是我范围内的一个字符串,其中不包括任何回车符。 As a result, every time I press "Enter", I get a little closer to the max length without impacting the character count. 结果,每当我按Enter键时,我都会接近最大长度,而不会影响字符数。

The result is that if the user presses enter 8 times, then he is blocked from typing once the character count says there are still 8 characters remaining. 结果是,如果用户按下Enter键8次,则一旦字符计数显示仍然剩余8个字符,就阻止了他键入。 How can I solve this using Angular? 如何使用Angular解决此问题?

You could recalculate the length every time the user presses a key using .keyup() (if you are using jQuery too) and replace all carriage returns using Regex like this string.replace(/[\\n\\r]/g, '') . 您可以在用户每次使用.keyup()按下键时重新计算长度(如果您也使用jQuery),并使用正则表达式替换所有回车符,例如string.replace(/[\\n\\r]/g, '') This would get rid of all the carriage returns and return the true length of the text. 这将消除所有的回车符并返回文本的真实长度。

将ng-trim =“ false”添加到元素:

<textarea class="form-control rows="7" ng-trim="false" ng-model="feedback" maxlength="1000"></textarea>

My solution was partially implementation of two of the proposed answers. 我的解决方案是部分实施两个建议的答案。 Firstly I needed to set ng-trim="false" as a part of the TextArea field. 首先,我需要将ng-trim="false"TextArea字段的一部分。 In this way, carriage returns were counted regularly, however, each carriage return counted as two characters. 通过这种方式,定期对回车进行计数,但是,每个回车都被视为两个字符。 This was, for our effort, deemed acceptable, but may not be the case for someone else using this solution. 在我们的努力下,这被认为是可以接受的,但使用此解决方案的其他人可能并非如此。

That is, is your text area has length 10, you could only press the carriage return 5 times before it prevents you from typing any more. 也就是说,如果您的文本区域的长度为10,则您只能按回车键5次才能阻止您再次输入。

In the controller, I had to create logic which emulated the very same character count as the one I just described above. 在控制器中,我必须创建逻辑来模拟与上述字符计数相同的逻辑。

$scope.adjustForReturn = function() {
    // 10 being the max length
    $scope.remainingCharacters = 10 - $scope.feedback.replace("\r\n", "").length;
    $scope.remainingCharacters = $scope.remainingCharacters - ($scope.feedback.split(/[\r\n]/).length - 1);

    // Since carriage return is 2 chars, if you have one left and press enter, it will display -1. This is a fix to that edge case
    if ($scope.remainingCharacters < 0) {
        $scope.remainingCharacters = 0;
    }
}

The above function was bound to the text area using ng-keyup="adjustForReturn()" so that whenever the user releases a key pressed, the remaining character count is re-evaluated (which works well from a UX perspective). 上面的函数是使用ng-keyup="adjustForReturn()"绑定到文本区域的,因此,每当用户释放按键时,都会重新评估剩余的字符数(从UX角度来看效果很好)。

I have not tested this extensively cross browser yet, this was all done in Chrome, I was made to understand that for one of my colleagues, the carriage return did in fact count as one character, so again, I want to re-emphasize this solution is probably not ideal and there is a little more work to be done around it, but it seems like it is 95% of the way there. 我还没有测试过这种广泛的跨浏览器,这一切都是在Chrome中完成的,我被理解为对于我的一位同事来说,回车符实际上算作一个字符,因此,我想再次强调一下解决方案可能并不理想,围绕它还需要做更多的工作,但是看来它已经完成了95%。

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

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