我有一个文本区域,可以预览键入的文本。然后我要计算行数,并用\ n或命中32char来定义新行。
<textarea class="form-control" id="inputText1" rows="4" maxlength="64" ng-model="textBase"></textarea>
<pre>{{textBase}}</pre>
Rows after break or 32char: {{(textBase ? textBase.split("\n").length : 0)}}
此后,它将计算每个中断后的行数。但是,我该如何添加使其在休息后还是在32char之后?
答案 0 :(得分:0)
This is very hard to handle using inline expressions.
I would suggest to use a ng-change and outsource the calculation to a controller function. I have create a working fiddle to solve this problem. Since I did not know your correct working flow (counting a row with 32 chars as one or two rows?, ...) you need to adjust this example to your own needs.
View:
<textarea class="form-control" id="inputText1" rows="4" maxlength="64" ng-model="textBase" ng-change="evaluateChange()"></textarea>
<pre>{{textBase}}</pre>
Rows after break or 32char: {{rowsAfterBreakOr32}}
Controller:
$scope.textBase = '';
$scope.rowsAfterBreakOr32 = 0;
$scope.evaluateChange = function() {
// calc row break or 32chars
var rowBreaks = $scope.textBase ? $scope.textBase.split("\n") : [$scope.textBase];
$scope.rowsAfterBreakOr32 = rowBreaks.length;
// iterate each row and count chars
for(var i=0; i<rowBreaks.length; i++) {
if(rowBreaks[i].length > 32) {
$scope.rowsAfterBreakOr32 += rowBreaks[i].match(/.{1,32}/g).length;
}
}
};