• 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 ng-Route

Javilon06

Fandom Organizer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
73
Likes
64
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

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

index.html

This is the main view of our app.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    Angular JS Toggle Active Class on Path Change using ng-Route</

    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

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-route.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('/')"

    ><a

    href

    =

    "#/"

    >

    Home</

    a

    ></

    li

    >
  25. <li

    ng-class

    =

    "active('/about')"

    ><a

    href

    =

    "#/about"

    >

    About</

    a

    ></

    li

    >
  26. <li

    ng-class

    =

    "active('/blog')"

    ><a

    href

    =

    "#/blog"

    >

    Blog</

    a

    ></

    li

    >
  27. </

    ul

    >
  28. </

    div

    >
  29. </

    div

    >
  30. </

    nav>
  31. <div

    class

    =

    "container"

    >
  32. <div

    ng-view></

    div

    >
  33. </

    div

    >
  34. <script

    src

    =

    "app.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

    html

    >

In here, we used Bootstrap navbar to create our menu and we use ng-class to toggle active function that will send our defined parameter. The active function will then compared the sent parameter to the current path and will return active if matched.

app.js

This contains our angular js script. .config is where we setup the each of our path and .controller is where active function is found.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ngRoute'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $routeProvider)

    {
  3. $routeProvider
  4. .when

    (

    '/'

    ,

    {
  5. templateUrl:

    'home.html'
  6. }

    )
  7. .when

    (

    '/about'

    ,

    {
  8. templateUrl:

    'about.html'
  9. }

    )
  10. .when

    (

    '/blog'

    ,

    {
  11. templateUrl:

    'blog.html'
  12. }

    )
  13. .otherwise

    (

    {
  14. redirectTo:

    '/'
  15. }

    )

    ;
  16. }

    )

    ;

  17. app.controller

    (

    'mainCtrl'

    ,

    function

    (

    $scope,

    $location)

    {
  18. $scope.active

    =

    function

    (

    path)

    {
  19. return

    (

    $location.path

    (

    )

    ===

    path)

    ?

    'active'

    :

    ''

    ;
  20. }
  21. }

    )

    ;

Creating our Path Templates

The ff. are the template for our route.

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 :)

 

450,632

323,159

323,168

Top