动态生成的下拉菜单无效

时间:2015-08-31 12:03:41

标签: angularjs twitter-bootstrap drop-down-menu angular-ui-bootstrap

我需要从后端获取一个复杂的下拉菜单,并在角度应用程序中显示它。除了一件事之外,一切都运行良好:动态生成的下拉菜单无效。

的index.html

<!DOCTYPE html>
<html ng-app="BlogApp">
<head>
    <title>Blog</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.28/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.28/angular-sanitize.min.js"></script>

    <script src="test.js"></script>
</head>
<body ng-controller="tagsCtrl" >

<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" ng-init="navCollapsed = true" ng-click="navCollapsed = !navCollapsed">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a id="a-blog-path" target="_self" class="navbar-brand" href="#">Blog</a>
    </div>

    <div class="collapse navbar-collapse" ng-class="!navCollapsed && 'in'" ng-click="navCollapsed=true">
        <ul class="nav navbar-nav" ng-bind-html="topLeft">
        </ul>

        <ul class="nav navbar-nav navbar-right">

            <li dropdown>
                <a class="dropdown-toggle" href="#"> test <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">finger</a></li>
                </ul>
            </li>

        </ul>
    </div>
</nav>

</body>
</html>

test.js

angular.module('BlogApp', ['ui.bootstrap', 'ngSanitize'],
  function($locationProvider){
    $locationProvider.html5Mode(true);
  })
  .controller('tagsCtrl', ['$scope', '$location', '$http', '$sce',
    function ($scope, $location, $http, $sce) {

      $scope.topLeft  = '<li><a class="dropdown-toggle" href="#"> test <b class="caret"></b></a><ul class="dropdown-menu"><li><a href="#">finger</a></li></ul></li>';

    }]);

我有plunker

中的代码

在此示例中,topLeft菜单下拉列表不起作用,但topRight菜单工作正常。

我如何解决这个问题?

谢谢!

UPD:已解决。 Plunker working demo

2 个答案:

答案 0 :(得分:-1)

的index.html

<div ng-controller="MyCtrl">
  Hello, {{name}}!
  <div ng-bind-html="my_html | to_trusted"></div>
</div>

app.js

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

    angular.module('myApp')
        .filter('to_trusted', ['$sce', function($sce){
            return function(text) {
                return $sce.trustAsHtml(text);
            };
        }]);

    function MyCtrl($scope) {
        $scope.name = 'Superhero';
        $scope.my_html = '<label><b>Hello </b> <input type="text" value="world !"></label>';
    }

请查看修复的Demo

答案 1 :(得分:-1)

我以另一种方式解决了这个问题。

HTML:

<!DOCTYPE html>
<html ng-app="BlogApp">
<head>
    <title>Blog</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.28/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>
    <script src="test.js"></script>


</head>

<body ng-controller="tagsCtrl" >

<nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" ng-init="navCollapsed = true" ng-click="navCollapsed = !navCollapsed">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a id="a-blog-path" target="_self" class="navbar-brand" href="#">Blog</a>
    </div>

    <div class="collapse navbar-collapse" ng-class="!navCollapsed && 'in'" ng-click="navCollapsed=true">

        <ul class="nav navbar-nav" >
            <li dropdown>
                <a class="dropdown-toggle" href="#"> test <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#">finger</a></li>
                </ul>
            </li>
        </ul>

        <ul class="nav navbar-nav navbar-right">
            <li dropdown  class="{{menuItem.cls}}" ng-repeat="menuItem in tMenu"
                ng-include="menuItem.href ? 'menu-single.html' : 'menu-dropdown.html'"></li>
        </ul>
    </div>
</nav>

<script type="text/ng-template" id="menu-dropdown.html">
    <a class="dropdown-toggle" href="{{menuItem.href}}">{{menuItem.name}} <b class="caret"></b></a>
    <ul class="dropdown-menu">
        <li class="{{menuItem.cls}}" ng-repeat="menuItem in menuItem.childs" ng-include="'menu-single.html'"></li>
    </ul>
</script>

<script type="text/ng-template" id="menu-single.html">
    <a href="{{menuItem.href}}">{{menuItem.name}}</a>
</script>

</body>
</html>

test.js:

angular.module('BlogApp', ['ui.bootstrap'],
  function($locationProvider){
    $locationProvider.html5Mode(true);
  })

  .controller('tagsCtrl', ['$scope', '$location', '$http',
    function ($scope, $location, $http) {

      $scope.tMenu = [
        {name: 'level00', href: '#', cls: 'active'},
        {name: 'level1', childs: [
          {name: 'level2', href: '#'},
          {name: 'level21', href: '#', cls: 'active'}
        ]},
        {name: 'level1', childs: [
          {name: 'level2', href: '#'},
          {name: 'level21', href: '#'}
        ]}
      ];

    }]);

plunker demo