If you're looking for a solution, check out this jsfiddle that does exactly what you need. Each time you click on an item in the list, it gets added to the textarea.
The key function here is a versatile directive that can be utilized in different controllers:
myApp.directive('txtArea', function() {
return {
restrict: 'AE',
replace: 'true',
scope: {data: '=', model: '=ngModel'},
template: "<textarea></textarea>",
link: function(scope, elem, attrs) {
scope.$watch('data', function(newVal, oldVal) {
if (newVal) {
scope.model += JSON.parse(newVal[0])[attrs.property];
}
});
}
};
});
To implement this directive, include the following code snippet in your HTML (ensure correct placement as shown in the jsfiddle):
<txt-area data="myModel" property="mail" placeholder="expected value" ng-model='model' ng-trim='false'></txt-area>
The myModel
data represents the selected item from the dropdown list, as described here.
The mail
property is the field used to extract values from the selected element.
The model
holds the content of the textarea, which can be used in various ways in your application.
Below is the complete relevant HTML segment:
<div ng-app='myApp'>
<div ng-controller="Main">
<p>Original</p>
<select multiple="multiple" ng-model="myModel">
<option ng-repeat="d in data" value="{{d}}">{{d.mail}}</option>
</select>
<txt-area data="myModel" property="mail" placeholder="expected value" ng-model='model' ng-trim='false'></txt-area>
<div>{{model}}</div>
</div>
</div>
Here are the controller and app sections:
var myApp = angular.module('myApp', []);
myApp.controller('Main', function($scope){
$scope.data = [{mail:"a@foo"},{mail:"b@foo"},{mail:"c@foo"}];
$scope.model = "";
});
Just remember to define/initialize each directive's model (e.g. $scope.model = ""
) as seen above, or it may initially appear undefined.