0

I wrote a code that enables me to post an input value (using AngularJS 1.3.5 $http.post) and save it into a db then display it in my html page. To get the new input value after clicking on save I have to refresh the page to display it. I must find a solution without using php and jQuery. I saw an answer Here: changing from $http.post to $xhr.post wasn't possible may be it's caused by the angularJs version I am using. What should I do?

<form ng-submit="save()">
  <input ng-model="stack"></input>
  <button type="submit">Save</button>
<p>{{stack}}</p> 
</form>
$scope.save = function(url, data1) {
            /* post to server */
            $http({
                url : url,
                data : {
                    "stack" : data1
                },
                method : "POST",
                headers : {
                    'Content-Type' : 'application/json'
                }
            }).success(function(data, status, headers, config) {
                $scope.stack = data.stack;
            }).error(function(data, status, headers, config) {
                $scope.status = status + ' ' + headers;
            });

        };

Notice that I am displaying the input value in my html page from the backend.

5
  • 1
    Give us more code please, like your controller code ... Commented Jun 23, 2016 at 9:05
  • isn't the new input value same as u sent it earlier , you can display that only. otherwise make a new ajax call in success handler of save() call Commented Jun 23, 2016 at 9:05
  • @ArnaudGueras I updated my question Commented Jun 23, 2016 at 9:19
  • What is the data returned by data.stack? Are you sure it has the updated data? Commented Jun 23, 2016 at 10:21
  • @BonMacalindong data.stack returns the input value to post to the backend Commented Jun 23, 2016 at 10:24

2 Answers 2

3

Bind the get call inside a function and call that function inside the same controller, this will update data without refresh.

  $scope.getData = function() {
        $http({
            method : 'GET',
            url : '/getData'         //any url
        }).then(function(response) {
            //success code

        })
    }
    $scope.getData();
Sign up to request clarification or add additional context in comments.

16 Comments

thank you for your answer. The call of get method in the controller caused an access allow control error. I updated my question with my controller code
Are you returning the updated object from the backend?
yeah after inserting it in the db I used the select request to call it from the db and get it in the web service and finally display it
So you are making a get call in the frontend? can you show me the get call?
modify something like this app.factory('myapp', [ '$http', function($http) { return { method : function($scope) { $http.get('Url').success(function(data) { $scope.stack=data; }).error(function(err) { $scope.stack=err; }); } } } ]); and call it like this in your controller and inject $scope into it myapp.method($scope)
|
1

Step 1: Make your request return the new data

Step 2: Use that data to replace the old data

Since you didn't provide much info about how the structure is I'll try to be generic as possible. Assuming you're updating a product and fetching its new price:

$scope.product = {
    name: 'Pop Rocks',
    price: 1.99 // can be changed from an input with `ngModel` or something
};

$scope.save = function() {
    // post new price
    $http.post('/api/products', $scope.product).then(function(response) {
        // response.data contains the JSON object for he product,
        // with new price/name/etc
        product = JSON.parse(response.data) // parse it into an object
        $scope.product = product // replace old $scope.product with new product
    });
}

$scope.doublePrice = function() {
    // using this will update the new price immediately -in the view-,
    // just to show you how binding works. It will not update DB
    $scope.product.price *= 2;
}

Assuming your view has something similar to this:

<strong>Price:</strong> {{ product.price | currency:"$" }}

Your changes should reflect thanks to 2 way binding!

3 Comments

thank you for your answer I tried to follow the steps but still have the same problem. I updated my question with the controller code
Does your data.stack actually hold the data you need? It should work. Try wrapping the assignment with a $timeout call ($timeout(function() { $scope.stack = data.stack}))
yeah it should hold the data to post to my web service . I tried with $timeout but I still have the same problem

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.