用ng重复或ng模型填充一个文本区域,这样它就不会用迭代的数据一个接一个地创建新的文本区域
filling a textarea with ng-repeat or ng-model so that it does not create a new textarea one on top of the other with the iterated data
im在尝试使用ng repeat用数据填充单个10行文本区域时遇到问题,而是为每个数据条目创建一个新的文本区域。我怎么能插入其中呢?我想使文本区域的大小与ng重复输入的条目数量动态
这是我的js文件
var WaitingRequisitionsController = function($scope, $rootScope, $modal, $window, items) {
$rootScope.title = 'Direct Requisitions';
$scope.items = items;
$scope.formatDate = function (x) {
return moment(x).format("M/D/YYYY");
};
};
这是我的html文件,特别是我试图插入的区域。
<div class="row ">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="form-group">
<label>Items</label>
<div ng-repeat="item in req.items">
<textarea ng-model="item.description" class="form-control" type="text" readonly="readonly"></textarea>
</div>
</div>
</div>
</div>
这做了同样的事情
<div class="row ">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="form-group">
<label>Items</label>
<textarea ng-repeat="item in req.items" class="form-control" type="text" readonly="readonly">{{item.description}}</textarea>
</div>
</div>
</div>
我把js改成这个
var WaitingRequisitionsController = function($scope, $rootScope, $modal, $window, items) {
$rootScope.title = 'Direct Requisitions';
//$scope.items = items;
$scope.formatDate = function (x) {
return moment(x).format("M/D/YYYY");
};
};
function TodoCtrl($scope) {
$scope.items = items.join(''n');
$scope.$watch('items', function (items) {
$scope.myArray = items.split(''n');
console.log('$scope.myArray', $scope.myArray);
});
}
和我的html看起来像这个
<!-- <div class="row ">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="form-group" ng-app>
<label>Items</label>-->
<div ng-app>
<div ng-controller="TodoCtrl">
<textarea rows={{rows}} class="form-control" ng-model="myStr" type="text" readonly="readonly"></textarea>
</div>
</div>
<!-- </div>
</div>
</div>-->
但如果我把它改成这样,什么都不会填充http://jsfiddle.net/U3pVM/8165/
它有效。。我不明白我需要item.description我不知道这是否与有关
将项目连接到一个字符串中,用换行符分隔
var WaitingRequisitionsController = function($scope, $rootScope, $modal, $window, items) {
$rootScope.title = 'Direct Requisitions';
$scope.items = items
.map(function (item) {
return item.description;
})
.join(''n');
$scope.rows = items.length;
$scope.formatDate = function (x) {
return moment(x).format("M/D/YYYY");
};
};
然后,您可以使用项数组的长度来设置文本区域上的行数。
<textarea type="text" class="form-controler readonly="readonly" rows="rows"
ng-model="items">
</textarea>
例如Plunker:http://plnkr.co/edit/SXSl4nHJi8ptufP6wTd4?p=preview
您正在重复<textarea>
(在其本身或通过包含的div
),以便它在item
的每个实例上迭代。您需要做的是将ng-repeat
全部去掉,并使用ng-model
来连接一个字符串,如ajs的答案所示。
相关文章:
- 使用javascript将两个文本框值相加到表中的另一个文本框中
- 在文本上方标记另一个文本
- 如何通过 JS 将内容从一个表单中的一个文本框复制到另一个表单中的另一个文本框
- 使用Javascript从一个文本区域逐行解析.subsr(0,6)到另一个文本区
- 任何编写文本文件并将其替换为上一个文本文件的方法
- html或java脚本代码在硬盘中创建一个文本文件
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 一个文本框,其中两个选项的值不同
- 在javascript中获取servlet值,并使用ajax将该值分配给一个文本框
- 如何从由“”分隔的字符串中提取最后一个文本|&”;
- 如何使用jquery对具有相同类的文本框的值求和并将其放入另一个文本框中
- 使用 jquery 将行值设置为另一个文本
- 从一个文本框执行代码,并在另一个文本框中以 HTML 显示输出
- 如何将文本插入到几个带有 id 的文本区域中的一个文本区域中
- 如何使用jQuery查找下一个文本区域
- jquery专注于asp.net mvc部分视图中的一个文本框
- 将数组中的数据作为段落添加到一个文本区域
- 当你有很多文本框时,如何从一个模态中只填充一个文本框
- Javascript/Php聊天-输入is'不能在一个文本框中工作
- 当文档准备就绪时,将数据从一个文本框复制到另一个