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

JavaScript - Create, Read, Delete, Update Functions Using AngularJS

heartsx

Threat Landscape Analyst
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
121
Likes
124
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Create, Read, Delete, Update Functions Using AngularJS. This code can create, read, delete, update data the when user click one the buttons. The code use AngularJS directives to call a function that divided into a different functionalities that manipulate the data entry within the array object. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "myModule"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    ng-controller=

    "myController"

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "containet-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://www.sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Create, Read, Delete, Update Functions Using AngularJS</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Full Name</

    label

    >
  21. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model =

    "newMember.name"

    /

    >
  22. </

    div

    >
  23. <div

    class

    =

    "form-group"

    >
  24. <label

    >

    Address</

    label

    >
  25. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model =

    "newMember.address"

    /

    >
  26. </

    div

    >

  27. <center

    ><button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click =

    "saveMember()"

    ><span

    class

    =

    "glyphicon glyphicon-save"

    ></

    span

    >

    Save</

    button

    ></

    center

    >
  28. </

    form

    >
  29. </

    div

    >
  30. <div

    class

    =

    "col-md-8"

    >
  31. <table

    class

    =

    "table table-bordered"

    >
  32. <thead

    class

    =

    "alert-info"

    >
  33. <tr

    >
  34. <th

    >

    Full Name</

    th

    >
  35. <th

    >

    Address</

    th

    >
  36. <th

    >

    Action</

    th

    >
  37. </

    tr

    >
  38. </

    thead

    >
  39. <tbody

    >
  40. <tr

    ng-repeat=

    "member in members"

    >
  41. <td

    >

    {{member.name}}</

    td

    >
  42. <td

    >

    {{member.address}}</

    td

    >
  43. <td

    ><button

    type

    =

    "button"

    data-toggle=

    "modal"

    data-target

    =

    "#update_member"

    ng-click=

    "selectMember(member)"

    class

    =

    "btn btn-sm btn-warning"

    ><span

    class

    =

    "glyphicon glyphicon-edit"

    ></

    span

    >

    Update</

    button

    >

    | <button

    type

    =

    "button"

    data-toggle=

    "modal"

    data-target

    =

    "#delete_member"

    ng-click =

    "selectMember(member)"

    class

    =

    "btn btn-sm btn-danger"

    ><span

    class

    =

    "glyphicon glyphicon-trash"

    ></

    span

    >

    Delete</

    button

    ></

    td

    >
  44. </

    tr

    >
  45. </

    tbody

    >
  46. </

    table

    >
  47. </

    div

    >
  48. </

    div

    >
  49. <div

    class

    =

    "modal fade"

    id

    =

    "update_member"

    tabindex

    =

    "-1"

    role=

    "dialog"

    aria-labelledby=

    "myModalLabel"

    >
  50. <div

    class

    =

    "modal-dialog"

    role=

    "document"

    >
  51. <div

    class

    =

    "modal-content"

    >
  52. <form

    >
  53. <div

    class

    =

    "modal-header"

    >
  54. <button

    type

    =

    "button"

    class

    =

    "close"

    data-dismiss=

    "modal"

    aria-label

    =

    "Close"

    ><span

    aria-hidden=

    "true"

    >

    &times;

    </

    span

    ></

    button

    >
  55. <h4

    class

    =

    "modal-title text-info"

    id

    =

    "myModalLabel"

    >

    Updating Member</

    h4

    >
  56. </

    div

    >
  57. <div

    class

    =

    "modal-body"

    >
  58. <div

    class

    =

    "form-group"

    >
  59. <label

    >

    Full Name</

    label

    >
  60. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model =

    "selectedMember.name"

    /

    >
  61. </

    div

    >
  62. <div

    class

    =

    "form-group"

    >
  63. <label

    >

    Address</

    label

    >
  64. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model =

    "selectedMember.address"

    /

    >
  65. </

    div

    >
  66. </

    div

    >
  67. <div

    class

    =

    "modal-footer"

    >
  68. <button

    class

    =

    "btn btn-success"

    data-dismiss =

    "modal"

    ><span

    class

    =

    "glyphicon glyphicon-edit"

    ></

    span

    >

    Update</

    button

    >
  69. </

    div

    >
  70. </

    form

    >
  71. </

    div

    >
  72. </

    div

    >
  73. </

    div

    >
  74. <div

    class

    =

    "modal fade"

    id

    =

    "delete_member"

    aria-hidden=

    "true"

    >
  75. <div

    class

    =

    "modal-dialog"

    >
  76. <div

    class

    =

    "modal-content"

    >
  77. <form

    >
  78. <div

    class

    =

    "modal-body"

    >
  79. <center

    ><h4

    class

    =

    "text-danger"

    >

    Are you sure you want to delete this record?</

    h4

    ></

    center

    >
  80. </

    div

    >
  81. <div

    class

    =

    "modal-footer"

    >
  82. <button

    class

    =

    "btn btn-danger"

    data-dismiss=

    "modal"

    ng-click=

    "deleteMember()"

    ><span

    class

    =

    "glyphicon glyphicon-check"

    ></

    span

    >

    Yes</

    button

    >
  83. <button

    class

    =

    "btn btn-success"

    data-dismiss=

    "modal"

    ><span

    class

    =

    "glyphicon glyphicon-remove"

    ></

    span

    >

    No</

    button

    >
  84. </

    div

    >
  85. </

    form

    >
  86. </

    div

    >
  87. </

    div

    >
  88. </

    div

    >
  89. </

    body

    >
  90. <script

    src

    =

    "js/angular.js"

    ></

    script

    >
  91. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  92. <script

    src

    =

    "js/jquery-3.2.1.min.js"

    ></

    script

    >
  93. <script

    src

    =

    "js/bootstrap.js"

    ></

    script

    >
  94. </

    html

    >

Creating the Main Function

This code contains the main function of the application. This code will create, read, delete, update a data when the button is clicked To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.
  1. var

    app =

    angular.module

    (

    "myModule"

    ,

    [

    ]

    )
  2. .controller

    (

    "myController"

    ,

    function

    (

    $scope)

    {

  3. $scope.newMember

    =

    {

    }

    ;
  4. $scope.clickedMembers

    =

    [

    ]

    ;


  5. $scope.members

    =

    [

    ]

    ;

  6. $scope.saveMember

    =

    function

    (

    )

    {
  7. $scope.members

    .push

    (

    $scope.newMember

    )

    ;
  8. $scope.newMember

    =

    {

    }

    ;
  9. }

    ;

  10. $scope.selectMember

    =

    function

    (

    member)

    {
  11. $scope.selectedMember

    =

    member;
  12. }

    ;

  13. $scope.deleteMember

    =

    function

    (

    )

    {
  14. $scope.members

    .splice

    (

    $scope.members

    .indexOf

    (

    $scope.selectedMember

    )

    ,

    1

    )

    ;
  15. }

    ;
  16. }

    )

    ;

There you have it we successfully created a Create, Read, Delete, Update Functions using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


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

452,292

323,348

323,357

Top