Angularjs select elements from list and display in textarea

微笑、不失礼 提交于 2019-12-04 21:02:07

I am not familiar with the plugin you are using, however, I have tried to achieve what you wanted using multiple attribute of select element.

<select multiple="multiple" ng-model="selectedValues">
      <option ng-repeat="mail in a" value="{{mail.name}}">{{mail.mail}}</option>

    </select>
    <textarea>{{selectedValues}}</textarea>

Please review the plunker at "http://plnkr.co/edit/u2sXnMkYSEYshAcLgyn7?p=preview"

Answer updated as per the comments below. OP is asking for a generic element to be reused on the page. There are possibly other/easier ways to do this, but I am extending the previous answer.

Set value attribute in each list element to person (this is needed for regular multi-select list, although may not be needed for nya-select):

<li nya-bs-option="person in myController.arrayCollection" value="{{person}}">

The ng-model myModel in the ordered list should contain the selections. We'll use that to render the content in the textarea. Add the following directive to the application:

myApp.directive('txtArea', function() {
    return {
        restrict: 'E',
        replace: 'true',
        scope: {data: '=', property: '@'},
        template: "<textarea readonly>{{result()}}</textarea>",
        link: function(scope, elem, attrs) {
            scope.result = function() {
                var ret = "";
                angular.forEach(scope.data, function(value, key) {
                    var suff = (key === scope.data.length - 1) ? '' : ', ';
                    ret += JSON.parse(value)[scope.property] + suff;
                });
                return ret;
            }
        }
    };
});

The directive is generic and can be reused across controllers. This also adds a comma after each intermediary value. Now replace the textarea element with:

<txt-area data="myModel" property="mail"></txt-area>

myModel is the model bound to the ordered list and mail is the property to use in the directive/filter.

Working jsfiddle with regular multi-select list.

Latest updated jsfiddle based on discussion below.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!