0

I have a edit button in a form, when user clicks on the edit button, and edit the form and clicks on save. It gets updated. But when the user reloads the page, it does not update the page. So, here is my code below. All I want is when user edits the text in the form, there text gets updated even they reload the page. (Like, I want the JSON file gets that request too, if possible). Thanks in advance.

    <div class="list-view">
  <form>
    <fieldset ng-disabled="inactive">

  <legend>Basic Info</legend>

  <b>First Name:</b>
  <input type="text" ng-model="people.first">
  <br>
  <b>Last Name:</b>
  <input type="text" ng-model="people.last">
  <br>
  <b>Email:</b>
  <input type="email" ng-model="people.email">
  <br>
  <b>Phone:</b>
  <input type="num"  ng-model="people.phone">
  <br>
  <b>Website:</b>
  <input type="text" ng-model="people.website">
  <br>
  <b>Education:</b>
  <input type="text" ng-model="people.education">
  <br>
  <b>Education Year:</b>
  <input type="text" ng-model="people.year">

  <br>
  <legend>Address</legend>

<b>Street:</b>
  <input type="text" ng-model="people.street">
  <br>
  <b>City:</b>
    <input type="text" ng-model="people.city">
    <br>
    <b>State:</b>
      <input type="text" ng-model="people.state">
      <br>
      <b>Zip:</b>
        <input type="text" ng-model="people.zip">
    <br>
    </fieldset>
    <button type="button" class="edit" ng-show="inactive" ng-click="inactive = !inactive">
          Edit
        </button>
    <button type="submit" class="submit" ng-show="!inactive" ng-click="save()">Save</button>



</form> 

app.js

    var app = angular.module("MyLab", ['ngRoute']);



app.controller('MyCtrl', function($scope, $rootScope) {
  $scope.inactive = true;



});

$scope.save = function() {
  $scope.people.push($scope.people.name);
}

Controller

        app.controller('MyController',['$scope', 'people', '$routeParams',
function($scope, people, $routeParams) {
  people.success(function(data) {
    $scope.people = data[$routeParams.id];

    $scope.save = function() {

      return people.editPeople()
      .then(function(data){
        $scope.datas = data.data; 
      })

    }


  });
}]);

Json file

    [
  {
    "id": "0",
    "first": "John",
    "last": "Doe",
     "title": "Family",
    "date": "Joined 4/2/17",
    "email": "[email protected]",
    "phone": "555-555-5555",
    "website": "www.google.com",
    "education": "Harvard",
    "year": "2008",
    "street": "123 Main Street",
    "city": "Los Angeles",
    "state": "CA",
    "zip": "1234567"


  },

] 

index.html

    <!doctype html>
<html ng-app="MyLab">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script src="https://code.angularjs.org/1.2.28/angular-route.min.js"></script>
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,300,700' rel='stylesheet' type='text/css'>


   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link href="css/main.css" rel="stylesheet" />



  </head>
  <body>
  <div class="header">
      <div class="container">
          <h3>My Page</h3>
      </div>
    </div>


    <div class="main">
      <div class="container">
          <div ng-view>
          </div>
      </div>
    </div>



    <script src="js/app.js"></script>

      <script src="js/MyController.js"></script>




  </body>
</html>

Grabbing json

     app.factory('people', ['$http', function($http) {
  var services = {
    editPeople : editPeople
  }
  return services
  function editPeople() {

    $http({
        method: "POST",
        url: "people.json",
        data: {
          people: $routerParams.id
        },
       headers : { 'Content-Type': 'application/x-www-form-urlencoded' }

    })

  } 
15
  • Where your $scope.people.name of you ? And push is method of array . I think $scope.people of you is object Commented Apr 20, 2017 at 23:20
  • and yes it is an object. BUT, can you show me of how to apply the save() or update() method to my code. Commented Apr 20, 2017 at 23:25
  • Can you provide full code ? Commented Apr 20, 2017 at 23:29
  • which part you need ? Commented Apr 20, 2017 at 23:29
  • that is my full code above Commented Apr 20, 2017 at 23:29

1 Answer 1

1

Here is example for update . You can use ajax to it . For example I use php in server-side

    .factory('people',...)
     var services = {
           editPeople : editPeople
}
      return services
     function editPeople(){
     $http({
                method: "POST",
                url: "model/update.php",
                data: {
                 people:$routeParams.id
                     },
               headers : { 'Content-Type': 'application/x-www-form-urlencoded' }

                })
      }

In ctrl

$scope.save = function(){
   return people.editPeople()
      .then(function(data){
         $scope.datas = data.data;
})
}

And display in html

ng-repeat ="data in datas"
{{data.name}}
Sign up to request clarification or add additional context in comments.

8 Comments

this should be in app.factory or in my app.js?
It you want use factory I will update it . And this code above is in controller . $scope.save is ng-click of button save of you
can you please update in factory, so I can see how this works.
apply to my app.factory above, if you can please
No you can't update it with out server-side language like php or python
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.