简体   繁体   中英

How to load html inside a div on menu click using angular?

I am new to angular, i have a menu and a container, when the user click the menu item i want the view to change dynamically. I have followed some samples but din't work. Here is my code,

1.Index.html 2.Dashboard.html

HTML:

 <ul id="side-menu" class="nav">
    <div class="clearfix"></div>
      <li class="active" ng-click="template='dashboard.html'"><a href="">
           <div class="icon-bg bg-orange"></div>
       <span class="menu-title">Dashboards</span>
       </a>
      </li>

app.js:

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

Here is the div:

<div class="page-content" ng-include src="template">
</div>

Looks like what you need is to add a routing mechanism (views are usually defined by route and not by a logical condition) Angular's way to make it clean is via routing module, it can be done with the ngRoute module or with ui-router module by the AngularUI team.

The idea is to define a state and attache a view to state, add a ui-view tag in your html and the view is dynamically injected into its parent wrapper:

app.js

var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/dashboard');

    $stateProvider          
        .state('dashboard', {
            url: '/dashboard',
            templateUrl: 'dashboard.html'
        })

});

index.html (please consider the ui-view tag as the html container)

<body ng-app="routerApp">

<!-- NAVIGATION -->
<nav class="navbar navbar-inverse" role="navigation">
    <div class="navbar-header">
        <a class="navbar-brand" ui-sref="#">AngularUI Router</a>
    </div>
    <ul class="nav navbar-nav">
        <li><a ui-sref="dashboard">Dashboard</a></li>
    </ul>
</nav>

<div class="container">

    <div ui-view></div>

</div>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>

</body>

To extend angular-routing-using-ui-router

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM