ng-if not functioning properly following the activation of the "Save Changes" button

When I click the edit change button, I am updating information and then hiding the form to show the updated details by clicking on the Save Changes button. My API successfully updates the information, but for some reason, ng-if does not seem to work after clicking the Save Changes button.

info.html file

<div class="panel-body" ng-if="userEnteredInfo">
    <div class="form-group">
        <dl class="dl-horizontal form-info-table">
            <dt>First Name:</dt>
            <dd>{{user.basicInfo.firstName}}
            </dd>
            <dt>Last Name:</dt>
            <dd>{{user.basicInfo.lastName}}</dd>
            <dt>Status:</dt>
            <dd>{{user.basicInfo.status}}</dd>
            <dt>Work Location:</dt>
            <dd>{{user.basicInfo.workLocation}}</dd>               
            <dt>Date of Birth:</dt>
            <dd>{{user.basicInfo.dateOfBirth}}</dd>
            <dt>Title:</dt>
            <dd>{{user.basicInfo.title}}</dd>
        </dl>
    </div>
</div>
<div class="panel-body form-panel-box" ng-if="makeChanges" ng-submit="saveChanges(user)">
    <form class="form-horizontal edit-changes-form">
        <div class="form-group">
            <label class="control-label col-sm-2" for="firstName">First Name:</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="firstName" ng-model="user.basicInfo.firstName">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="lastName">Last Name:
              </label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="lastName" ng-
                 model="user.basicInfo.lastName"> 
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="status">Status:
               </label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="status" ng-
                   model="user.basicInfo.status">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="Work Location">Work 
               Location:</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="work" ng-
                  model="user.basicInfo.location">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="dateOfBirth">Date of 
              Birth</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="dateOfBirth" ng-
                  model="user.basicInfo.dateOfBirth">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-2" for="title">Title:</label>
            <div class="col-sm-3">
                <input type="text/number" class="form-control" id="title" 
                  ng-model="user.basicInfo.title">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-3">
                <button type="submit" class="btn btn-default">Save 
                 Changes</button>
            </div>
        </div>
    </form>

info.js file

app.controller('info',function($scope,$localStorage,authService,$http){
      $scope.user = $localStorage.userObject;

      $scope.userEnteredInfo = true;
      $scope.editChanges = function(){
        $scope.makeChanges = true;
        $scope.userEnteredInfo= false;

    }
    //Edit changes for updating information//
    $scope.saveChanges = function(userData){
        $scope.updatedInfoDetails = {
            firstName: userData.basicInfo.firstName,
            lastName: userData.basicInfo.lastName,
            status: userData.basicInfo.status,
            WorkLocation: userData.basicInfo.location,
            dateOfBirth: userData.basicInfo.dateOfBirth,
            title: userData.basicInfo.title
        }

    authService.postUpdatedInfo($scope.updatedInfoDetails)
    .then(function(response){
        if(response.status == 200){
             $scope.updatedInfo = "You have successfully updated the changes";

            };
        });
        $scope.userEnteredInfo = false;
        $scope.makeChanges = true;
        console.log("success");

    };
});

authService.js file

app.service('authService', function($localStorage,$http) {
    this.postUpdatedInfo = function(userChangedInfo){
        console.log('the user data is: ', userChangedInfo);
        var urlForChangingInfo = "/api/users/" + $localStorage.getUserId;
        return $http({
            method:'PUT',
            url:urlForChangingInfo,
            data:userChangedInfo,
            headers:{"x-access-token":$localStorage.authToken}
        });
    };    
});

Answer №1

never set makeChanges to false,

give this a go,

calling the authService's postUpdatedInfo function with $scope.updatedInfoDetails parameter
.then checking if the response status is 200:
if so, setting $scope.updatedInfo variable to "You have successfully updated the changes"
setting $scope.userEnteredInfo and $scope.makeChanges to false
logging "success"

Answer №2

you need to make sure that $scope.userEnteredInfo is properly set to true. Here is a suggestion of how you can update the code:

authService.postUpdatedInfo($scope.updatedInfoDetails)
.then(function(response){
    if(response.status == 200){
         $scope.updatedInfo = "Changes have been successfully updated";
         $scope.userEnteredInfo = true;//ensure it's set to true here
         $scope.makeChanges = true;
         console.log("success");
        }
        else
        {
         // handle errors if response status is not 200
         $scope.userEnteredInfo = false;
         $scope.makeChanges = false;
         console.log("error");
        };
    });


};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Explore numerous databases using mongoosastic

Currently in my Node.js application, I am utilizing Mongoosastic to fetch data from ElasticSearch : Article.search({ "match_all": {} }, function (err, results) { console.log(results.hits.hits); Post.search({ "match_all": {} }, function (err, r ...

Adjust speed on various HTML5 video players

I'm looking to slow down all the HTML5 video players on my page to 0.5x speed. Currently, I have a JavaScript snippet that only affects one video player at a time. <script type="text/javascript"> /* play video twice as fast */ document.quer ...

What are the best methods for improving the rendering performance of multiple sphereGeometry objects in three.js?

Looking to improve the rendering performance of sphereGeometry in my three.js program, as it is currently a bottleneck. Here is the JavaScript code I am using: var sphereThree = []; for(var idSphere = 0; idSphere < numSphere; idSphere++){ var spher ...

Navigating a sortable list using jQuery

I have integrated the "sortable" script from on my web application to create a sortable list of items. While the sorting functionality works perfectly, I now need to update my server with the new order whenever changes are made. To capture the sorting ev ...

When attempting to transfer data to a CSV file from my Firebase database, I encounter an issue where the

I am facing an issue with exporting data from my Firebase Firestore to a .csv file. I have followed all the necessary steps, but whenever I try to add the values for export, they show up as undefined. While I am not an expert in React and consider myself ...

In need of clarification on the topic of promises and async/await

I have been utilizing Promises and async/await in my code, and it seems like they are quite similar. Typically, I would wrap my promise and return it as needed. function someFetchThatTakesTime(){ // Converting the request into a Promise. return new ...

Issue with the iteration process while utilizing Async.waterfall in a for-loop

This snippet may seem simple, but it represents a real issue in my current code base. When attempting to log the index of a for-loop within an async.waterfall function, I am encountering a peculiar situation where the index value is 2 instead of expected ...

Sorting the Czech alphabet with the help of Tablesorter

Utilizing the tablesorter characterEquivalents extension as outlined in this documentation. I have created a similar extension for Czech characters, but the sorting is not functioning correctly for certain characters - like \u017d $.extend( $.tables ...

The inline filter in angularJS is failing to function as expected within the ng-repeat loop

I am currently working with angularJS version 1.2.14. Within my interface, the ng-repeat function successfully displays the information as intended. However, there seems to be an issue with the filter functionality. Despite entering text into the input bo ...

Issue with debugging capabilities in Javascript on VSCode has been detected

UPDATE: I'm seeking guidance on configuring VSCode for debugging Javascript. While I'm familiar with JavaScript functioning in a browser, I find it tedious to rely solely on console.log(). I am looking to debug and step through my code effectivel ...

The Protractor tool (node:9208) threw an UnhandledPromiseRejectionWarning due to an ElementNotVisibleError, indicating that the element is not interactable. Despite this

I am attempting to interact with an element using protractor but encountering the following error (node:9208) UnhandledPromiseRejectionWarning: ElementNotVisibleError: element not interactable (Session information: chrome=69.0.3497.92) (Driver informa ...

The Ajax request functions flawlessly on Mozilla but encounters issues on Chrome, causing confusion as it occasionally works

I am using a PHP file with a class and function to store data in the database, accessed via AJAX. While everything works smoothly in Mozilla, Chrome seems to be causing some issues. Strangely, sometimes it works fine, but other times it fails for no appare ...

It is not possible to recycle a TinyMCE editor that is embedded in a popup

Having a frustrating issue with the TinyMCE Editor plugin embedded within a Fancybox pop-up window. I have a list of objects with Edit links that trigger an AJAX call to retrieve content from the server and place it in a <textarea>. A TinyMCE editor ...

Find the flowplayer when the page loads

Is there a way to make a flowplayer seek at the page load without it resetting? I've tried this: $(document).ready(function() { $f(0).seek(5); }); also tried: $(document).ready(function() { while(!$f(0).isLoaded()) { $f(0).seek(5); } }); ...

The jQuery function is returning an inaccurate value for the height of

In my Cocoa (Mac) application, I am utilizing a Webview and trying to determine the accurate height of a document. While the typical method webview.mainFrame.frameView.documentView.bounds.size.height works well in most cases, I encountered an issue with on ...

creating a function within an object that allows for chaining

My goal is to create a chainable object, but I am struggling to implement this in a function. This is the functionality I desire: $donate.ga('testing').go(value); The code for my object currently appears as follows: var $donate = { ga: fu ...

Next.js API Endpoint Call Resulting in Empty Object Returned by Fetch Function

Having an issue with making an API call in Next.js to delete an item from the database. I'm using the "body" field of the fetch to send a string to the API. The fetch call is within a Next.JS page, and the API endpoint is located in the API folder gen ...

Journeying through a grid in AngularJS

My current project involves displaying a table where users can click on specific cells to highlight them. Now, I want to take it a step further and enable navigation of the highlighted cells using arrow keys on the keyboard. For example, if the user press ...

Utilizing nested grouping in mongoose schemas

I am facing a challenge while attempting to execute a nested group query in MongoDB. Is there a way to group data by both date and campaign ID, ensuring that each campaign ID contains a nested array of creatives with their respective data (views and clicks ...

Having trouble properly refreshing Bootstrap scrollspy that is spying on the <body> element

I've been looking at a post on Bootstrap's scrollspy component (found here). In my code, I initialize a new scrollspy to track the body element using: $("body").scrollspy({ offset: 25 }); As my code progresses, I make AJAX requests that add or ...