• We just launched and are currently in beta. Join us as we build and grow the community.

Angular JS Toggle Active Class on Path Change using Ui-Router

CookedMods

Anime Production Enthusiast
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
133
Likes
68
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

I've used CDN for Bootstrap, Angular JS and Ui-Router so you need internet connection for them to work.

index.html

This is the main page of our app.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    Angular JS Toggle Active on Path Change using Ui-Router</

    title

    >
  5. <meta

    charset

    =

    "utf-8"

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  8. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"

    ></

    script

    >
  9. </

    head

    >
  10. <body

    ng-controller=

    "mainCtrl"

    >
  11. <nav class

    =

    "navbar navbar-default"

    >
  12. <div

    class

    =

    "container"

    >
  13. <div

    class

    =

    "navbar-header"

    >
  14. <button

    type

    =

    "button"

    class

    =

    "navbar-toggle collapsed"

    data-toggle=

    "collapse"

    data-target

    =

    "#bs-example-navbar-collapse-1"

    aria-expanded=

    "false"

    >
  15. <span

    class

    =

    "sr-only"

    >

    Toggle navigation</

    span

    >
  16. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  17. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  18. <span

    class

    =

    "icon-bar"

    ></

    span

    >
  19. </

    button

    >
  20. <a

    class

    =

    "navbar-brand"

    href

    =

    "www.sourcecodester.com"

    >

    SourceCodeSter</

    a

    >
  21. </

    div

    >
  22. <div

    class

    =

    "collapse navbar-collapse"

    id

    =

    "bs-example-navbar-collapse-1"

    >
  23. <ul

    class

    =

    "nav navbar-nav"

    >
  24. <li

    ng-class

    =

    "active('/home')"

    ><a

    ui-sref=

    "home"

    >

    Home</

    a

    ></

    li

    >
  25. <li

    ng-class

    =

    "active('/about')"

    ><a

    ui-sref=

    "about"

    >

    About</

    a

    ></

    li

    >
  26. <li

    ng-class

    =

    "active('/blog')"

    ><a

    ui-sref=

    "blog"

    >

    Blog</

    a

    ></

    li

    >
  27. </

    ul

    >
  28. </

    div

    >
  29. </

    div

    >
  30. </

    nav>
  31. <div

    class

    =

    "container"

    >
  32. <div

    ui-view></

    div

    >
  33. </

    div

    >
  34. <script

    src

    =

    "app.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

    html

    >

We have use Bootstrap to create our navbar and we put ng-class which toggles our active function and we pass the path of the menu as a parameter.

app.js

This contains our angular js scripts. .config is where we set the routes of our app and in .controller, we have our active function which returns active whenever the passed parameter is the same as the current path.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ui.router'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $stateProvider,

    $urlRouterProvider)

    {

  3. $urlRouterProvider.otherwise

    (

    '/home'

    )

    ;

  4. $stateProvider

  5. .state

    (

    'home'

    ,

    {
  6. url:

    '/home'

    ,
  7. templateUrl:

    'home.html'
  8. }

    )

  9. .state

    (

    'about'

    ,

    {
  10. url:

    '/about'

    ,
  11. templateUrl:

    'about.html'

    ,

  12. }

    )

  13. .state

    (

    'blog'

    ,

    {
  14. url:

    '/blog'

    ,
  15. templateUrl:

    'blog.html'

    ,
  16. }

    )

    ;

  17. }

    )

    ;

  18. app.controller

    (

    'mainCtrl'

    ,

    function

    (

    $scope,

    $location)

    {
  19. $scope.active

    =

    function

    (

    path)

    {
  20. return

    (

    $location.path

    (

    )

    ===

    path)

    ?

    'active'

    :

    ''

    ;
  21. }
  22. }

    )

    ;

Creating our Paths

The ff are the templates for the paths in this tutorial.

home.html
  1. <h4

    >

    This is the Home Page</

    h4

    >
  2. <p

    >

    See that the <b

    >

    Home Tab</

    b

    >

    is active</

    p

    >

about.html
  1. <h4

    >

    This is the AboutPage</

    h4

    >
  2. <p

    >

    See that the <b

    >

    AboutTab</

    b

    >

    is active</

    p

    >

blog.html
  1. <h4

    >

    This is the Blog Page</

    h4

    >
  2. <p

    >

    See that the <b

    >

    Blog Tab</

    b

    >

    is active</

    p

    >

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top