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

Angular JS Routing using UI-Router

Serpentine

Social Content Amplifier
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
149
Likes
63
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.

Why choose ui-router rather than angular-route?

You can visit this page if you want a more detailed instruction on the difference of the two but to sum up, ui-router can provide nested views and multiple views.

index.html

This is the main page of our app.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <title

    >

    Angular JS Routing 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-app=

    "app"

    >
  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"

    ui-sref=

    "#"

    >

    SourceCodeSter</

    a

    >
  21. </

    div

    >
  22. <div

    class

    =

    "collapse navbar-collapse"

    id

    =

    "bs-example-navbar-collapse-1"

    >
  23. <ul

    class

    =

    "nav navbar-nav"

    >
  24. <li

    ><a

    ui-sref=

    "home"

    >

    Home</

    a

    ></

    li

    >
  25. <li

    ><a

    ui-sref=

    "about"

    >

    About</

    a

    ></

    li

    >
  26. <li

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

    class

    =

    "row"

    >
  34. <div

    class

    =

    "text-center"

    >
  35. <p

    >

    A tutorial by <a

    href

    =

    "https://www.sourcecodester.com/users/nurhodelta2017"

    >

    nurhodelta_17</

    a

    ></

    p

    >
  36. <p

    >

    For more tutorials, visit <a

    href

    =

    "https://www.sourcecodester.com"

    >

    SourceCodeSter</

    a

    ></

    p

    >
  37. </

    div

    >
  38. </

    div

    >
  39. </

    div

    >
  40. <script

    src

    =

    "js/app.js"

    ></

    script

    >
  41. <script

    src

    =

    "js/controllers/movieController.js"

    ></

    script

    >
  42. <script

    src

    =

    "js/controllers/dataController.js"

    ></

    script

    >
  43. <script

    src

    =

    "js/controllers/blogController.js"

    ></

    script

    >
  44. </

    body

    >
  45. </

    html

    >

app.js

This is the main angular js script of our app.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ui.router'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $stateProvider,

    $urlRouterProvider)

    {

  3. $urlRouterProvider.otherwise

    (

    '/home'

    )

    ;

  4. $stateProvider

  5. // HOME STATES AND NESTED VIEWS
  6. .state

    (

    'home'

    ,

    {
  7. url:

    '/home'

    ,
  8. templateUrl:

    'partials/home.html'
  9. }

    )

  10. // nested with custom controller
  11. .state

    (

    'home.anime'

    ,

    {
  12. url:

    '/anime'

    ,
  13. templateUrl:

    'partials/anime.html'

    ,
  14. controller:

    function

    (

    $scope)

    {
  15. $scope.animes

    =

    [

    'Shokugeki no Soma'

    ,

    'Haikyuu'

    ,

    'Nanatsu no Tazai'

    ,

    'Asterisk'

    ]

    ;
  16. }
  17. }

    )

  18. // nested with external controller
  19. .state

    (

    'home.movie'

    ,

    {
  20. url:

    '/movie'

    ,
  21. templateUrl:

    'partials/movie.html'

    ,
  22. controller:

    'movieCtrl'
  23. }

    )

  24. // nested with string data
  25. .state

    (

    'home.quote'

    ,

    {
  26. url:

    '/quote'

    ,
  27. template:

    'Chance favors the prepared mind'

    ,

  28. }

    )

  29. // ABOUT PAGE AND MULTIPLE NAMED VIEWS
  30. .state

    (

    'about'

    ,

    {
  31. url:

    '/about'

    ,
  32. views:

    {
  33. ''

    :

    {

    templateUrl:

    'partials/about.html'

    }

    ,
  34. 'firstColumn@about'

    :

    {

    template:

    'This is the first column!'

    }

    ,
  35. 'secondColumn@about'

    :

    {
  36. templateUrl:

    'partials/second_table.html'

    ,
  37. controller:

    'dataCtrl'
  38. }
  39. }

  40. }

    )

  41. // SIMPLE PAGE
  42. .state

    (

    'blog'

    ,

    {
  43. url:

    '/blog'

    ,
  44. templateUrl:

    'partials/blog.html'

    ,
  45. controller:

    'blogCtrl'
  46. }

    )

    ;

  47. }

    )

    ;

movieController.js

This is the controller for our movie.html

  1. 'use strict'

    ;

  2. app.controller

    (

    'movieCtrl'

    ,

    function

    (

    $scope)

    {
  3. $scope.movies

    =

    [

    'Marvels Movies'

    ,

    'DC Movies'

    ,

    'Science Fiction'

    ,

    'Detective Movies'

    ]

    ;
  4. }

    )

    ;

dataController.js

This is the controller for our second_table.html

  1. 'use strict'

    ;

  2. app.controller

    (

    'dataCtrl'

    ,

    function

    (

    $scope)

    {
  3. $scope.friends

    =

    [
  4. {
  5. firstname:

    'Gemalyn'

    ,
  6. lastname:

    'Cepe'
  7. }

    ,
  8. {
  9. firstname:

    'Julyn'

    ,
  10. lastname:

    'Divinagracia'
  11. }

    ,
  12. {
  13. firstname:

    'Rupert'

    ,
  14. lastname:

    'Dikoalam'
  15. }
  16. ]

    ;
  17. }

    )

    ;

blogController.js

This is the controller for our blog.html

  1. 'use strict'

    ;

  2. app.controller

    (

    'blogCtrl'

    ,

    function

    (

    $scope)

    {
  3. $scope.blogs

    =

    [
  4. {
  5. title:

    'Angular JS Crud Operation'

    ,
  6. link:

    'https://www.sourcecodester.com/php/11909/angularjs-crud-search-sort-and-pagination-phpmysqli.html'
  7. }

    ,
  8. {
  9. title:

    'Angular JS Progress Bar'

    ,
  10. link:

    'https://www.sourcecodester.com/tutorials/javascript/11928/angularjs-progress-bar-phpmysqli.html'
  11. }

    ,
  12. {
  13. title:

    'Angualr JS Deleting Multiple Rows'

    ,
  14. link:

    'https://www.sourcecodester.com/tuto...rjs-delete-multiple-rows-using-phpmysqli.html'
  15. }
  16. ]

    ;
  17. }

    )

    ;

home.html

As per app.js script code, this is treated as our index page.

  1. <div

    class

    =

    "jumbotron text-center"

    >
  2. <h1

    >

    This the Home Page</

    h1

    >
  3. <p

    >

    In this page is an example of <span

    class

    =

    "text-danger"

    >

    nested</

    span

    >

    views.</

    p

    >

  4. <div

    class

    =

    "text-center"

    >
  5. <h3

    >

    My Favorites</

    h3

    >
  6. </

    div

    >
  7. <a

    ui-sref=

    ".anime"

    >

    Anime</

    a

    >

    ||
  8. <a

    ui-sref=

    ".movie"

    >

    Movie</

    a

    >

    ||
  9. <a

    ui-sref=

    ".quote"

    >

    Quote</

    a

    >
  10. </

    div

    >

  11. <div

    ui-view>

    This will be overwritten by nested views</

    div

    >

anime.html

This is one of the nested views.

  1. <ul

    >
  2. <li

    ng-repeat=

    "anime in animes"

    >

    {{ anime }}</

    li

    >
  3. </

    ul

    >

movie.html

This is the other nested view.

  1. <ul

    >
  2. <li

    ng-repeat=

    "movie in movies"

    >

    {{ movie }}</

    li

    >
  3. </

    ul

    >

about.html

This is our about page that contains multiple views.

  1. <div

    class

    =

    "jumbotron text-center"

    >
  2. <h1

    >

    The About Page</

    h1

    >
  3. <p

    >

    In this page is an example of <span

    class

    =

    "text-danger"

    >

    multiple</

    span

    >

    and <span

    class

    =

    "text-danger"

    >

    named</

    span

    >

    views.</

    p

    >
  4. </

    div

    >

  5. <div

    class

    =

    "row"

    >
  6. <div

    class

    =

    "col-sm-6"

    >
  7. <h3

    >

    First Column</

    h3

    >
  8. <div

    ui-view=

    "firstColumn"

    ></

    div

    >
  9. </

    div

    >
  10. <div

    class

    =

    "col-sm-6"

    >
  11. <h3

    >

    Second Column</

    h3

    >
  12. <div

    ui-view=

    "secondColumn"

    ></

    div

    >
  13. </

    div

    >
  14. </

    div

    >

second_table.html

This is one of the multiple views.

  1. <table

    class

    =

    "table table-striped table-bordered"

    >
  2. <thead

    >
  3. <tr

    >
  4. <th

    >

    Firstname</

    th

    >
  5. <th

    >

    Lastname</

    th

    >
  6. </

    tr

    >
  7. </

    thead

    >
  8. <tbody

    >
  9. <tr

    ng-repeat=

    "friend in friends"

    >
  10. <td

    >

    {{ friend.firstname }}</

    td

    >
  11. <td

    >

    ${{ friend.lastname }}</

    td

    >
  12. </

    tr

    >
  13. </

    tbody

    >
  14. </

    table

    >

blog.html

Lastly, this is our blog page that contains a simple view.

  1. <div

    class

    =

    "jumbotron text-center"

    >
  2. <h1

    >

    The Blog Page</

    h1

    >
  3. <p

    >

    In this page is an example of a Simple Route</

    p

    >
  4. </

    div

    >

  5. <div

    class

    =

    "row"

    >
  6. <div

    class

    =

    "col-xs-4 col-xs-offset-4"

    >
  7. <table

    class

    =

    "table table-striped table-bordered"

    >
  8. <thead

    >
  9. <tr

    >
  10. <th

    >

    Title</

    th

    >
  11. </

    tr

    >
  12. </

    thead

    >
  13. <tbody

    >
  14. <tr

    ng-repeat=

    "blog in blogs"

    >
  15. <td

    ><a

    href

    =

    "{{ blog.link }}"

    >

    {{ blog.title }}</

    a

    ></

    td

    >
  16. </

    tr

    >
  17. </

    tbody

    >
  18. </

    table

    >
  19. </

    div

    >
  20. </

    div

    >

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