0

This is what i have on my controller:

$scope.order = [];
var values = CartService.cart;

angular.forEach(values, function (value, key) {
    $scope.order.push(value.cart_item_name);
});

This part is for the Textarea box:

$scope.payTrans = {
    myOrder: --->equals to something to get all the data
}

And this the html textarea box:

<textarea  style="height: 500px;"
           class="bookingtextareamargin"
           placeholder="Comments"
           ng-model="payTrans.myOrder">
    This where i want to show all the data like :
    Item 1
    item 2
    Item 3
    Item 4
</textarea>
Harmelodic
  • 5,682
  • 1
  • 28
  • 31
joe mrema
  • 1
  • 6

1 Answers1

0

Just use join (js), and \n for newline

$scope.payTrans.myOrder = $scope.order.join('\n');

html:

<div ng-controller="MainCtrl">
 <textarea id="textarea" ng-model="payTrans.myOrder">      
 </textarea>
</div>

controller:

var app = angular.module('myApp', []);

app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.order = [];
    var values = [{
        cart_item_name: 'item1'
    },
    {
        cart_item_name: 'item2'
    },
    {
        cart_item_name: 'item3'
    },
    {
        cart_item_name: 'item4'
    }    
    ];

    angular.forEach(values, function (value, key) {
        $scope.order.push(value.cart_item_name);
    });

    console.log('s', $scope.order);

    $scope.payTrans = {
        myOrder: $scope.order.join('\n')
        };

}]);
Shlomi Levi
  • 3,114
  • 1
  • 23
  • 35
  • Its not working i get error `TypeError: Cannot set property 'myOrder' of undefined` – joe mrema Nov 02 '16 at 20:23
  • Hey bro, Thank you So Much Now i will never this now. On this `$scope.payTrans = { myOrder: $scope.order.join('\n') };` I was missing $scope.order.join('\n') This is how i was calling it just --- > order then got something like undefined – joe mrema Nov 02 '16 at 22:26
  • 2 days I was trying to figure this out. Thank you. – joe mrema Nov 02 '16 at 22:28
  • Now I see this going to look very messy in the database is there away I could set each item to be on it's Row? – joe mrema Nov 02 '16 at 23:03
  • you welcome. if it's helpful for you should can mark this question as answered :). about you last comment - I do not understand what you mean.. what you want to save to database? the text from textarea or from array? – Shlomi Levi Nov 03 '16 at 09:41
  • yeah to save the array, each item to have it's own row and id in the database, because now i'm just saving it as a single piece of text. it's not easy to find which item that has been added. it's just messy. I don't know how to do that. – joe mrema Nov 03 '16 at 13:04