JavaScript Dynamicly added dropdown menu in AngularJS + Bootstrap dynamically adding dropdown using angularjs,dynamically add dropdown in javascript,

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class.
The code:

angular.module('myapp', ['ui.bootstrap'])
.factory("menuService", ["$rootScope", function($rootScope) {
    "use strict";

    return { 
        menu: function() {
            $rootScope.globalMenu;
        },    
        setMenu: function(menu) {
            $rootScope.globalMenu = menu;
        }
    };

}])
.controller("MainController", ["$scope", "menuService",
    function($scope, menuService){

        menuService.setMenu([{href:"#", label:"Dropdown",
                                dropdown:[{href:"/edit", label:"Edit"}]},
                             {href:'/', label:'test'}]);

        $scope.bodyText = "Some text";


    }]);

This is the code in html

    <ul class="navbar-nav nav navbar-left">
        <li ng-repeat="menu_element in globalMenu" ng-class="{dropdown: menu_element.dropdown != undefined}">
            <a ng-target='_blank' href="{{menu_element.href}}" ng-class="{'dropdown-toggle': menu_element.dropdown != undefined}">
                {{menu_element.label}}
                <b class="caret" ng-if="menu_element.dropdown != undefined"></b>
            </a>
            <ul ng-if="menu_element.dropdown != undefined" class="dropdown-menu">
                <li ng-repeat="sub_element in $parent.menu_element.dropdown">
                    <a ng-target='_blank' href="{{sub_element.href}}">{{sub_element.label}}</a>
                </li>
            </ul>
        </li>
    </ul>

Link to plunker: http://plnkr.co/edit/pgH35mmsjLJqV4yJuSYq?p=preview

So what I want to do is the same or similar as for jQuery, there I would run $("li.dropdown").dropdown() after adding whole ul>li blocks. I'm new to Angular and I want to make this in the angular way.

I read about directives, how to use them. But I couldn't find how to apply directive in runtime. I've read about transclude: element in a directive (ui.bootstrap.dropdownToggle) doesn't have it enabled. I'm sure that there is a easy way, but couldn't find it myself...

Answer:1

Solved!

I've finally made it with ng-if and ng-repeat-start. With help in comments, I've found that ng-class does not run directives.

    <ul class="navbar-nav nav navbar-left">
        <span ng-repeat-start="menu_element in globalMenu"></span>
        <li ng-if="menu_element.dropdown !== undefined">
              <a ng-target='_blank' href="{{menu_element.href}}" class="dropdown-toggle">
                  {{menu_element.label}}
                  <b class="caret" ></b>
              </a>
              <ul class="dropdown-menu">
                  <li ng-repeat="sub_element in $parent.menu_element.dropdown">
                      <a ng-target='_blank' href="{{sub_element.href}}">{{sub_element.label}}</a>
                  </li>
              </ul>
        </li>
        <li ng-if="menu_element.dropdown === undefined">
              <a ng-target='_blank' href="{{menu_element.href}}">
                {{menu_element.label}}
              </a>
        </li>
        <span ng-repeat-end></span>
    </ul>

Working example on Plnkr. Something happened with the css on Plunker, yesterday it was working... but still it works.

Answer:2

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

i want to show a pop up with red color for some words i want to show variable "keyword name" in red color as well as " Todays Position" word . These both words should be in red color font. ...

  1. write alert in javascript
  2. write alert in php
  3. write alert in c#
  4. write alert in html
  5. write alert in jquery
  6. write alert in jsp
  7. write alert in code behind c#
  8. write in alert
  9. response.write with alert message

Here is the problem. The company I work for has a number of sites, and many of them use the same jQuery UI widgets, like datepicker. I would really like to have just one copy of each widget in an ...

Here is the problem. The company I work for has a number of sites, and many of them use the same jQuery UI widgets, like datepicker. I would really like to have just one copy of each widget in an ...

  1. link javascript file to html
  2. link javascript file to another javascript file
  3. link javascript file to html page
  4. link javascript file in asp.net
  5. link javascript file
  6. link javascript file to html5
  7. link javascript file php
  8. javascript link file download
  9. link external javascript file
  10. link external javascript file to html
  11. link external javascript file to html page
  12. javascript link js file
  13. django link javascript file
  14. dreamweaver link javascript file
  15. wordpress link javascript file
  16. link to javascript file in different folder
  17. link one javascript file to another
  18. w3schools link javascript file
  19. link to javascript file in head
  20. mvc link javascript file

I am trying to add a span class of "pink" to the end of a string. I can get this to work with one element, But when the page loads more than one, It keeps returning the very first element only. I ...

I am trying to add a span class of "pink" to the end of a string. I can get this to work with one element, But when the page loads more than one, It keeps returning the very first element only. I ...

  1. jquery each target class

I'm trying to filter a list of json data (https://api.myjson.com/bins/5a2aw) using an Angularjs filter. When I just use filter exact matching does not take place and for 1 shows results with id = 1,10,...

I'm trying to filter a list of json data (https://api.myjson.com/bins/5a2aw) using an Angularjs filter. When I just use filter exact matching does not take place and for 1 shows results with id = 1,10,...