Updates to the AngularJS model are not appearing in the user interface

Despite executing the controller code, the values in the UI remain unchanged. The initial values are displayed without any issue. I've attempted to call $scope.$apply() at the end of each function (submit and transfer), but it didn't resolve the issue (it only resulted in an error with the $http.post()). What might I be overlooking here?

app.controller('RegisterController', ['$scope','$http', function($scope,$http) {  
    $scope.user = {
        email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d0d5dcd8dff1d3ddd0d99fd2dedc">[email protected]</a>',
        password1: '1qaz2wsx',
        password2:'1qaz2wsx',
        password:'1qaz2wsx',
        username:'Admin',
        profile_picture:'',
        dobDay:'12',
        dobMonth:'12',
        dobYear:'1993',
        date_of_birth:'',
        stateMessage:'',
        messageColor:'white',
    };
    
    var transfer = function(){
        $http.post('http://localhost/cw/index.php/rest/resource/user/action/create',$scope.user).then(function successCallback(response) {
            if(response.data.status!=null){
                if(response.data.status=='success'){
                    $scope.user.stateMessage = 'success';
                    $scope.user.messageColor = "green";
                }
                else if(response.data.status=='failure'){
                    $scope.user.stateMessage = response.data.message;
                    $scope.user.messageColor = "red";
                }
                
            }
        },function errorCallback(response) {
            $scope.user.stateMessage = "Error occured.";
            $scope.user.messageColor = "red";
        });
        
    };
    
    $scope.submit = function(){
        $scope.user.stateMessage="";
        $scope.user.messageColor="white";
        var proceed = true;
        
        if(!validateFields()){
            $scope.user.stateMessage = "All fields must be filled!";
            $scope.user.messageColor = "red";
            proceed = false;
        }
        
        if(validateDate($scope.user.dobDay,$scope.user.dobMonth,$scope.user.dobYear)){
            $scope.user.date_of_birth= $scope.user.dobYear+"-"+$scope.user.dobMonth+"-"+$scope.user.dobDay;
        }else {
            proceed = false;
            $scope.user.stateMessage = "Date is invalid!";
            $scope.user.messageColor = "red";
        }
            
        if($scope.user.password1!=$scope.user.password2){
            proceed = false;
            $scope.user.stateMessage = "Passwords don't match!";
            $scope.user.messageColor = "red";
            
        }else $scope.user.password = $scope.user.password1;
        if(proceed){
            var file = document.getElementById('filePicker').files[0];
            reader = new FileReader();

            reader.onloadend = function(e) {
                $scope.user.profile_picture = JSON.stringify(e.target.result);
                $scope.$apply();
                console.log($scope.user.profile_picture);
                transfer();
            }
            reader.readAsDataURL(file);
        } 
        
        function validateFields(){
            /*some form validation*/
            return true;
        }
        
        function validateDate(day,month,year){
          /*some date validation*/
            return true;
        }
       
    }
}]);

HTML code

 <div ng-controller="RegisterController">
        <span style="background-color:{{user.messageColor}}"><h4>{{user.stateMessage}}</h4></span>
    </div>
<table style="text-align: left" ng-controller="RegisterController">
                            <tr>
                                <td>
                                    Username
                                </td>
                                <td>
                                    <input type="text" ng-bind="user.username" ng-model="user.username">
                                </td>       <td>&nbsp;</td><td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>
                                    Email
                                </td>
                                <td>
                                    <input type="email" ng-bind="user.email" ng-model="user.email">
                                </td>       <td>&nbsp;</td><td>&nbsp;</td>
                           </tr>
                           <tr>
                                <td>
                                    Password
                                </td>
                                <td>
                                    <input type="password" ng-bind="user.password1" ng-model="user.password1">
                                </td>       <td>&nbsp;</td><td>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>
                                    Retype password
                                </td>
                                <td>
                                    <input type="password" ng-bind="user.password2" ng-model="user.password2">
                                </td>       <td>&nbsp;</td><td>&nbsp;</td>    
                           </tr>
                           <tr>
                                <td>
                                    Date of Birth
                                </td>
                                <td>
                                    <input type="text" ng-bind="user.dobDay" ng-model="user.dobDay" value="DD" size="2" maxlength="2">
                                    <input type="text" ng-bind="user.dobMonth" ng-model="user.dobMonth" value="MM" size="2" maxlength="2">
                                    <input type="text" ng-bind="user.dobYear" ng-model="user.dobYear" value="YYYY" size="4" maxlength="4">
                                </td>    
                           </tr>
                           <tr>
                                <td>
                                    Profile picture
                                </td>
                                <td>
                                    <input id="filePicker" type="file" ng-bind="user.profile_picture" ng-model="user.profile_picture" accept="image/*">
                                    
                                </td>    
                           </tr>
                           <tr>
                               <td></td>
                               <td style="float:left">
                                    <button ng-click="submit()">Submit</button>
                               </td>
                           </tr>
                        </table>

Answer №1

It appears that using the same controller in multiple instances is causing issues for me. I found success by relocating the status div into the table assigned to the controller. While there are methods to share data between controllers using a service, I am choosing not to explore this option as it is solely for coursework purposes.

AngularJS: Sharing Data of a Single Controller Across Two Separate Areas on the Page

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

What steps are needed to switch colors after a loop has been clicked?

I have a loop setup like this: data: { show: false } .test { hight: 10px; background-color: red; } .test2 { hight: 15px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div v-for="value in da ...

Mastering React hooks: A guide to effectively updating and rendering elements

Each time I click the delete button, it seems to only remove the last element instead of the specific one based on index. Is there a better way to achieve this without changing from <input defaultValue={name} /> to <input value={name} /> in t ...

Transfer JSON response information into a dropdown menu

Here is the JSON data that I have. I want to create a dropdown menu containing all Role names from the JSON response provided below: { "json": { "response": { "servicetype": "1", "functiontype": "10011", "statuscode": "0", "st ...

A Vue.js component modifies one store state variable but leaves another unchanged

Currently developing a Vue 3 application that utilizes Vuex and the Composition API. Encountered an issue while working on it. A component is supposed to display elements based on the state of two store variables. One is an array, and the other is a bool ...

The functionality of making Slim POST requests is currently not functioning as expected within the Ionic

An issue is arising with my app that makes calls to a REST API using POST and GET methods. The app I'm developing with Ionic works perfectly when emulated using the command: ionic serve --lab However, when running the app on an actual device, calls ...

Can one utilize HTML's .querySelector() method to target elements by xlink attribute within an SVG document?

Here is the scenario: <body> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <a xlink:href="url"></a> </svg> </body> Now, can you utilize the HTML DOM's .querySe ...

The necessity of ExpressJS

After reviewing the Express.JS 4.x API documentation, I became intrigued by their setup process. Here's my interpretation of how it works: In the provided sample code snippet from the Express.JS 4.x API, the express module is first imported and stored ...

Using AJAX to dynamically populate PHP form inputs from HTML

I'm trying to create a simple HTML form where users can input their information and have it sent to a PHP file using JavaScript with AJAX. However, I'm encountering an issue where the values from the form are not being included in the email that ...

Read and insert an object into another object without using an array

I am currently attempting to verify and add an object within another object's details from a JSON file. Here is what my JSON structure looks like: { "stylesheet": { "attribute-set": [ { "attribute": [ { "_na ...

Sending a sound recording to the express js server with the help of multer

I'm currently working on a project where I need to record audio and save it in my local directory (uploads folder) using express js and multer. The recording part is working fine with mic-recorder-to-mp3, but I'm facing an issue with saving the r ...

Two separate tables displaying unique AJAX JSON response datasets

As a beginner in javascript, I am facing a challenge. I want to fetch JSON responses from 2 separate AJAX requests and create 2 different tables. Currently, I have successfully achieved this for one JSON response and table. In my HTML, I have the followi ...

What is the best way to remove text from a box when a user clicks on it?

After successfully placing G in the selected box upon clicking it, I now want to work on removing it when clicked again. I'm encountering an issue with my current code - can anyone help me identify what's wrong and suggest a solution? Below is ...

When using $dialogs.create on my website, a popup form appears with specific formatting limitations dictated by the defining code

When a user clicks a button on my website, a function in the controller is triggered. Here is the function that runs when the button is pressed: $scope.launch = function(idOfSpotOfInterest, schedOfSpotOfInterest){ var dlg = null; dlg = $dialogs. ...

Tips for preventing the need to create a function within a loop

Currently, I am in the process of collecting data through REST calls. I have created a function that accesses a "directory" endpoint to retrieve a list of individuals. While I can easily obtain details about their children, I need to make individual AP ...

Experiencing Error: "Oops! encountering [$injector:unpr] error in angularjs despite correctly including my dependencies."

Here is a glimpse of my factory settings: app.factory('AuthenticationService',['$http', function ($http, $localStorage) { var AuthenticationService = {}; var api = 'http://del1-vm-kohls:8080/Survey' ; Authe ...

Vue's span function is yielding the promise object

In my Vue component, I am using the function getOrderCount to fetch the number of orders from a specific URL and display it in one of the table columns. <div v-html="getOrderCount(user.orders_url)"></div> async getOrderCount(link) { ...

Angularfire2: Access Denied Error When User Logs Out

When utilizing the following method: login() { this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()) .then(() => { this.router.navigate(['']); }); } An error occurs during logout: zone.js:915 Unca ...

The keyup event fails to trigger for the search input in datatables when ajax is being used

When loading a page with a jQuery datatable via AJAX, I am aiming to implement custom filtering when the user starts typing in the default filter provided by datatables. The custom logic needs to be applied when the keyup event is triggered. Since AJAX is ...

I keep encountering an attach() error every time I try to close a modal that contains a vee-validated form

Every time I try to close a bootstrap modal (using bootstrap-vue) that includes a vee-validated "update" form with vue.js, I encounter the following error: main.js:477686 Uncaught (in promise) Error: [vee-validate] Validating a non-existent field: "#35". ...

When working with a set of objects, consider utilizing jQuery's InArray() method to effectively handle

Within my Javascript code, I am working with an array of Calendar objects. Each Calendar object contains an array of CalendarEvent objects. Every CalendarEvent object holds properties for Date and Name. I am looking to determine if a specific date exist ...