0

I have this kind of data

$scope.data = [{
  "production_list": [
    {"product": "google\n"},
    {"product": "stackoverflow\n"},
    {"product": "angular\n"}
   ]
}]

How can I display it in textarea like this?

<textarea>
  google
  stackoverflow
  angular
</textarea>

What I tried is

<tr ng-repeat="list in data">
  <td>
    <textarea ng-repeat="x in data.production_list">
       {{x.product}}
    </textarea>
  </td>
</tr>

The output is

<textarea>
  google
</textarea>
<textarea>
  stackoverflow
</textarea>
<textarea>
  angular
</textarea>

And is this a possible to have an increment? Because instead of ng-repeat its better to make it like this

<textarea value="data.production_list[$index].product">
</textarea>

however $index is not working its value is 0

5
  • do you need two way binding here or simple one time binding Commented Nov 8, 2017 at 4:33
  • @jitender can you give me an example sir? Commented Nov 8, 2017 at 4:36
  • Mean do you want to change the values inside controller also if you made any change in textarea or you want to simple display them in textarea? Commented Nov 8, 2017 at 4:38
  • Just display in textarea Commented Nov 8, 2017 at 4:39
  • ok check my answer then Commented Nov 8, 2017 at 4:41

3 Answers 3

3

Instead of writing it as a scope function you can make use of filter so that you can easily re-use code across module.

var app = angular.module('myApp', []);
app.filter('joinAttrs', function() {
    return function(list, attrName) {
        var joinedStr = "";
        for(var i=0; i<list.length; i++){
          joinedStr += list[i][attrName];
        }
        return joinedStr;
    };
});
app.controller('DemoCtrl', function($scope) {
    $scope.data = [{
      "production_list": [
        {"product": "google\n"},
        {"product": "stackoverflow\n"},
        {"product": "angular\n"}
       ]
    }]
});
textarea {
  height: 100px;
  width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="DemoCtrl">
  <div ng-repeat="list in data">
    <textarea>{{list.production_list | joinAttrs:'product'}}</textarea>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You could create a function that joins the data from the array first, then use that as your ng-model.

In your controller:

 $scope.textareaData = data[0].production_list
  .map(function(item) {
    return item.product;
  })
  .join('');

Then your textarea would be:

<textarea ng-model="textareaData"></textarea>

This will output each product inside your textarea.

4 Comments

what would happen if there are multiple object inside data?
I guess it would depend on their data structure. Right now, it is just an object wrapped in an array, so we the production_list is at position 0.
yup it should just wanted to suggest you to give a flexible answer
Thanks, appreciate the tip!
1

Create an function inside your controller and use it in text area something like

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {
$scope.data = [{
  "production_list": [
    {"product": "google\n"},
    {"product": "stackoverflow\n"},
    {"product": "angular\n"}
   ]
}]
$scope.textAreaValues = function(data){
  var retVal=  data.reduce(function(a,b){
  return {product: a.product + b.product};
  });
  return retVal.product;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<table>
<tr ng-repeat="list in data">
  <td>
    <textarea>
     {{textAreaValues(list.production_list)}}
    </textarea>
  </td>
</tr>
</table>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.