0

I have this basic fiddle which is a cut down version of what I have on my site.

I was wondering if there is a way to bind the table rows dynamically to a text area to create a ng-model composed of serveral other ng-model data?

I would like to to have each line in a list or something which I could then add additional text to from a text input element and have that all bound to a text area. Changes to either would be reflected in the form preview.

Is this possible?

4

1 回答 1

1

首先,你的小提琴有一个小错误,requestQty应该是一个由 itemId 索引的对象,或者一个索引是项目 ID 的数组,但它是两者的混合。

现在关于您的问题,您确实可以在 textarea 和您的模型之间保持绑定,但不是直接使用,ng-model因为 textarea 处理字符串,并且您的数据是对象。

解决这个问题的一种方法是使用两个不同的变量,一个model对象和一个stringModel字符串,并使用自定义 $watchers 保持两者同步。

您可以在此处查看该解决方案的实现:

http://jsfiddle.net/dscace5q/1/

$scope.$watch('model', function(m) {
    $scope.stringModel = angular.toJson(m, 2);
}, true);

$scope.$watch('stringModel', function(s) {
    $scope.model = angular.fromJson(s);      
});
于 2015-05-13T02:13:10.990 回答