0

Having one select tag in html now i want to make select tag dynamic through angularjs so that i can get drop downs from one select options and also want to give different ng-options for every new drop down created

"<div>
  <label>dropdown1</label>
  <select ng-options=''></select>
</div>"
2
  • Here: stackoverflow.com/questions/34200484/… Commented Dec 18, 2015 at 12:29
  • Also, you have to look at the angular official docs. There are a lot of examples about what you need with the html and js files attached. Commented Dec 18, 2015 at 13:38

3 Answers 3

3

To be honest, your question is for me a little unclear, but it may help you:

<div ng-repeat="object in myObjects">
  <label>{{object.name}}</label>
  <select ng-options="object.myOptions"></select>
</div>

this in js:

function AppCtrl ($scope) {
  $scope.myObjects = {
    "Select1": {
      "name": "dropdown1",
      "myOptions": [
        "one",
        "two"
      ]
    }, ....
Sign up to request clarification or add additional context in comments.

Comments

2

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

  app.controller('myController2', function($scope){
      $scope.statuses = [{
        id: 1,
        name: "First Value"        
    }, {
        id: 2,
        name: "Second Value"        
    }, {
        id: 3,
        name: "Third Value"        
    }, {
        id: 4,
        name: "Fourth Value"        
    }];
    $scope.selected_status = 3;
  
  })

  app.directive('bsDropdown', function ($compile) {
    return {
        restrict: 'E',
        scope: {
            items: '=dropdownData',
            doSelect: '&selectVal',
            selectedItem: '=preselectedItem'
        },
        link: function (scope, element, attrs) {
            var html = '';
            switch (attrs.menuType) {
                case "button":
                    html += '<div class="btn-group"><button class="btn button-label btn-info">Action</button><button class="btn btn-info dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>';
                    break;
                default:
                    html += '<div class="dropdown"><a class="dropdown-toggle" role="button" data-toggle="dropdown"  href="javascript:;">Dropdown<b class="caret"></b></a>';
                    break;
            }
            html += '<ul class="dropdown-menu"><li ng-repeat="item in items"><a tabindex="-1" data-ng-click="selectVal(item)">{{item.name}}</a></li></ul></div>';
            element.append($compile(html)(scope));
            for (var i = 0; i < scope.items.length; i++) {
                if (scope.items[i].id === scope.selectedItem) {
                    scope.bSelectedItem = scope.items[i];
                    break;
                }
            }
            scope.selectVal = function (item) {
                switch (attrs.menuType) {
                    case "button":
                        $('button.button-label', element).html(item.name);
                        break;
                    default:
                        $('a.dropdown-toggle', element).html('<b class="caret"></b> ' + item.name);
                        break;
                }
                scope.doSelect({
                    selectedVal: item.id
                });
            };
            scope.selectVal(scope.bSelectedItem);
        }
    };
});
<link href="http://st.pimg.net/cdn/libs/bootstrap/2.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.angularjs.org/1.4.8/angular.js"></script>
  <script src = "http://st.pimg.net/cdn/libs/jquery/1.8/jquery.min.js">
</script>
<script src = "http://st.pimg.net/cdn/libs/bootstrap/2/js/bootstrap.min.js">
</script>

   <body ng-app="pof">
  
    <div ng-controller="myController2 as myCtrl2">
     
      <select ng-init="selected_status = statuses[1].id" ng-model="selected_status" ng-options="status.id as status.name for status in statuses"></select>
      
      
<!--<bs-dropdown data-menu-type="button" select-val="selected_status = selectedVal"
preselected-item="selected_status" data-dropdown-data="statuses"></bs-dropdown>  &nbsp; --> Selected Value : {{selected_status}}

    </div>    
   
</body>

Comments

0

I don't know what your model looks like but maybe something like this:

<div ng-repeat="item in items">
  <label>{{item.label}}</label>
  <select ng-options="item.options"></select>
</div>

In your controller you would have an array $scope.items that contain all your dropdowns/select elements and their options.

$scope.items = [
  {'label':'Item 1','options':{"option 1.1","option 1.2"}},
  {'label':'Item 2','options':{"option 2.1","option 2.2"}}
];

1 Comment

can i pass function in ng-option?

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.