my json object, "flowcomponents" contains string (name) , array of strings (edition). example:
{ "_id": "553e87f3205465e83b46999b", "name": "flowcomponent_contactcombination_edition", "__v": 0, "edition": [ "billing", "billingdelivery", "default", "deliveryaddressonly", "deliverybillinglicensee", "deliverybillinglicenseewithwrapper", "deliverylicensee", "deliveryonlycopytoall", "licenseedelivery", "sassdefault", "sassdeliveryonlycopytoall" ] }
i need add/concat edition existing flowcomponents object. form has dropdown names of existing flowcomponents, , text area makes array of each line of text:
<form ng-submit="addtoexistingflowcomponent()"> <div class="interact"> <select ng-model="existingname" chosen options="flowcomponents" ng-options="item item.name item in flowcomponents" data-placeholder="select flow component..."> </select> </div> <div class="interact"> <label class="interact-label">enter each edition on new line.</label> <textarea id="text_area" placeholder="edition" ng-model="existingeditionlist" ng-list=" " ng-trim="false"></textarea> </div> <button type="submit">submit</button> </form>
this add edition method in controller:
$scope.addtoexistingflowcomponent = function(){ if(!$scope.existingname || $scope.existingname === '') { return; } var existingfc = $scope.existingname._id; sendappdata.postedition( existingfc, { edition: $scope.existingeditionlist }); $scope.existingname = ''; $scope.existingeditionlist = ''; };
and method posts data server:
this.postedition = function(existingfc, newedition) { return $http.post('/new-flow-component', newedition).success(function(data){ flowcomponents.push(data); }); };
the problem is, pushing data new object rather adding existing object. i'm able pass _id of existing object existingfc parameter, can't figure out how inside of function(data) in order push correct edition array.
i modified code new editions text area append selected edition array. removed posting server , have submitted "new" editions appended edition array. here plunker example: http://plnkr.co/edit/u2be9sdlictj9deiwkjc?p=preview
hope you
controller:
app.controller('mainctrl', function($scope) { $scope.flowcomponents = [{ "_id": "553e87f3205465e83b46999b", "name": "flowcomponent_contactcombination_edition", "__v": 0, "edition": [ "billing", "billingdelivery", "default", "deliveryaddressonly", "deliverybillinglicensee", "deliverybillinglicenseewithwrapper", "deliverylicensee", "deliveryonlycopytoall", "licenseedelivery", "sassdefault", "sassdeliveryonlycopytoall" ] }] $scope.addtoexistingflowcomponent = function(){ if(!$scope.existingname || $scope.existingname === '') { return; } var existingfc = $scope.existingname._id; var newedition = { edition: $scope.existingeditionlist }; console.log($scope.existingname); console.log(newedition); for(var i=0;i<$scope.existingeditionlist.length;i++){ $scope.existingname.edition.push($scope.existingeditionlist[i]); } console.log($scope.flowcomponents); $scope.existingname = ''; $scope.existingeditionlist = ''; }; });
your html:
<body ng-controller="mainctrl"> <form ng-submit="addtoexistingflowcomponent()"> <div class="interact"> <select ng-model="existingname" chosen options="flowcomponents" ng-options="item item.name item in flowcomponents" data-placeholder="select flow component..."> </select> </div> <div class="interact"> <label class="interact-label">enter each edition on new line.</label> <textarea id="text_area" placeholder="edition" ng-model="existingeditionlist" ng-list=" " ng-trim="false"></textarea> </div> <button type="submit">submit</button> </form> </body>