What is preventing these AngularJS applications from functioning simultaneously?

I have a fully functioning AngularJS app that I developed as a standalone "CreateUser" widget. Now, I am working on creating a second widget called "ViewUsers," which will display a table of current users (with the intention of connecting them or keeping them separate based on the user's page).

However, while my first app is running smoothly, the second app seems to encounter issues. Even a simple

<input ng-model="test" />{{test}}
does not work.

**UPDATE: It appears that calling $scope.loadUsers(); is causing the error. How can I invoke a loading function to run in the constructor function?

Here is a Fiddle link and the code snippet: http://jsfiddle.net/YYcna/

HTML Structure (excluding html/head tags)

<body ng-app>
    <fieldset>
        <legend>Create new user</legend>
        <form ng-submit="createNewUser()" ng-controller="CreateUsers" enc-type="multipart/form-data" method="POST" action="">
            <select ng-model="selectedType" ng-options="type as type for type in types" name="type"></select>
            <input style="display:block;" ng-repeat="field in fields[selectedType]" type="text" name="{{field.value}}" ng-model="formData[field.value]" placeholder="{{field.name}}" />
            <input type="submit" value="Create" />
        </form>
    </fieldset>

    <fieldset>
        <legend>All users</legend>
        <div ng-controller="ViewUsers">
            <input type="text" ng-model="test" />{{test}}
        </div>
    </fieldset>
</body>

JAVASCRIPT code snippet

/**
 * User creation controller. 
 * 
 * @param {type} $scope
 * @param {type} $http
 * @returns {undefined}
 */
function CreateUsers($scope, $http, $rootScope){
    // Controller logic here...
}
 

Answer №1

Attempting to execute the function before it's actually defined is not feasible using this function assignment syntax.

Consider using the following approach instead:


/**
 * View users controller
 *
 * @param {type} $scope
 * @returns {undefined}
 */
function ViewUsers($scope, $http){
    $scope.users = [];
    $scope.url = '/users/getUsers'
    $scope.test = 'checken';

    $scope.loadUsers = function(){
        $http({
            method: 'GET',
            url: $scope.url
        }).success(function(data, status){
            $scope.status = status;
            $scope.data = data;
            console.log($scope.data);
        }).error(function(data, status){
            $scope.data = data || 'Request failed';
            $scope.status = status;
            console.log($scope.data);
        });
        console.log('attempted to get users');
    };
    $scope.loadUsers();
    console.log('loaded view users');
}

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

Using JavaScript to convert date and time into ISO format

Similar Question: How do I output an ISO-8601 formatted string in Javascript? I've been attempting to change a date and time input into an ISO format but keep encountering the error .toISOString is undefined. It seems like I must be overlooking s ...

Generating a safe POST connection with express.js

Is there a simple method to generate a link for submitting a POST request using Express.js or a plugin? This approach can also be employed to enhance security for important actions like user deletion, including CSRF protection. In some PHP frameworks lik ...

Is it possible to delete a section of the URL within an anchor tag prior to the #anchor

My webpage contains a link that looks like this: <li class="jump"><a href="http://example.com/#about">About Me</a></li> I am interested in using jQuery to eliminate the 'http://example.com/' section of any URL found with ...

When you assign the page results from the scrape-it npm to a variable, it will return a Promise with the status of "pending."

I recently downloaded scrape-it from NPM as a dependency and I'm trying to store the results in a variable instead of using a callback function. When I run the code snippet provided in the scrape-it documentation: var myVar = scrapeIt("http://ionicab ...

The term "Ext" has not been recognized or defined

Currently, I am facing an issue while attempting to integrate a TinyMCE plugin with ExtJs. I found a helpful demo example at this link, which I followed closely. However, my implementation is failing as I am receiving an error message stating "Ext is not ...

What are the steps to modify the relative URL when using Express proxy?

To prevent the common CORS issue with client-side JavaScript code, I utilize a Node.js Express server. The server is configured with the following code: var app = express(); app.all('/Api/*', function (req, res) { proxy.web(req, res, { ...

Is utilizing the correct use case for a Bunyan child logger?

I've recently started exploring bunyan for logging in my nodejs application. After giving it a try, everything seems to be functioning quite smoothly. Initially, I overlooked a section on log.child, but now I am eager to understand its usage. It appea ...

Running globally installed node modules on Windows 7 is not supported

Note: Despite scouring various posts on this issue, I have not found a solution that works for me. That's why I am reaching out here. Problem: I am facing an issue while trying to set up the http-server package on my Windows 7 system using the comman ...

Troubleshooting problems with sending data in Jquery Ajax POST Request

I've spent a considerable amount of time searching for a solution to why my post request isn't sending its data to the server. Oddly enough, I can successfully send the request without any data and receive results from the server, but when attemp ...

Is there a way to dynamically import a JSON file within an ECMAScript module?

Currently, I am attempting the following in my code: let filePath = '../../data/my-file.json' import inputArray from filePath assert { type: 'json' } The outcome of this operation is as follows: file:///.../script.mjs:5 import inputArr ...

Error: The object referenced does not have a function called 'findById' within its methods when trying to export

I have come across a number of similar questions on this platform, but most of them seem to be related to HTML. My issue involves encountering an error while submitting a login form in Java. Here is a Very Simple Save Function: var model = require(' ...

Troubleshooting issue with Angular's ng-class functionality malfunctioning

I am currently working on displaying a class using ng-class in the box element of my HTML code. However, it does not seem to be functioning as expected. Can someone please help me identify what I might be doing wrong? Check out this link for reference. ...

There seems to be a glitch preventing Highchart from functioning within a jQuery function

Hello there! I'm currently utilizing the highchart API to generate graphs in a loop. for(i=1;i<10;i++) { xseries = "{'INCOPAV','B&M','SGS-ETSA'}"; yseries = "[{name: &apos ...

Tips on retrieving an input value from a dynamic list

I am struggling to retrieve the correct value with JavaScript as it always shows me the first input value. Any help would be greatly appreciated! Thank you in advance! <html> <head> </head> <body> <?php while($i < $forid){ ...

Ways to provide input parameters to a method upon clicking an element without using the HTML onclick attribute

Is there a way to pass data between pages without redirecting to the next.php page every time a button is clicked? One suggestion is to remove the onclick attribute from the button: <button class="submit-btn"></button> and use this jQuery fu ...

When sending a request from Vue.js using Axios to PHP, the issue arises that the .value property is

There is a chat box with bb smileys underneath. Clicking on the smileys adds them to the text in the input box. However, when using axios to post, the array is empty. Manually entering the smiley bbcode works fine. <input id="txtName" @keyup.enter="ad ...

Retrieve the concealed method's name within the immediately invoked function expression (IIFE

This Meteor sever code has a private method called printFuncName within an IIFE. However, when this method is invoked from a public method, it results in the following error: TypeError: Cannot read property 'name' of null I am curious to unde ...

"Can you explain the method by which reveal.js adjusts the size of

I have been exploring the resizing behavior of elements in reveal.js and trying to understand how they dynamically adjust. If you resize the page height, you can observe that the elements shrink up to a certain extent along with the page. However, when in ...

Is there a way to showcase the legend in a pie chart with two columns?

Is there a way to show the legend in two columns within the pie chart? Here is my JSfiddle link for reference: http://jsfiddle.net/Cp73s/2185/ itemStyle: { width:200, font: 'Helvetica Neue' ...

Navigate using jquery on a keyboard starting from a designated input text field

I'm currently developing an internal web application and I want to incorporate keyboard navigation into it. After doing some research, it looks like using JavaScript is the most effective way to achieve this. Below is a snippet of what I have come up ...