How to invoke a function from a different ng-app in AngularJS

I have 2 ng-app block on the same page. One is for listing items and the other one is for inserting them. I am trying to call the listing function after I finish inserting, but so far I haven't been successful in doing so. I have researched how to call functions within the same ng-app, but what I really need is to call a function from another ng-app.

Below is my code. Any help would be greatly appreciated.

<body>
<div id="UserControllerDiv" ng-app="UserTableApp" ng-controller="UsersController" class="form-group">
    <br /><br />
    <p>Search : <input type="text" ng-model="search" /></p>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>First Name</th>
                <th>Middle Name</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="u in users | filter:search">
                <td>{{ u.FirstName }} </td>
                <td>{{ u.MiddleName }} </td>
            </tr>
        </tbody>
    </table>
</div>
<br />
<div id="UserFormDiv" ng-app="UserFormApp" ng-controller="UserFormCtr" class="form-group">
    <table class="form-group">
        <tr>
            <td>First Name</td>
            <td> : </td>
            <td><input type="text" ng-model="FirstName" class="" /> </td>
        </tr>
        <tr>
            <td>Middle Name</td>
            <td> : </td>
            <td><input type="text" ng-model="MiddleName" /> </td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td>
                <button ng-click="submit()">Save</button>
            </td>
        </tr>
    </table>
</div>
<script>
    var userTableApp = angular.module("UserTableApp", []);
    userTableApp.controller("UsersController",
        function UsersController($scope, $http) {
            $http.get("http://localhost:10160/UserService/Users")
            .success(function (response) { $scope.users = response; });
        });
    var userFormApp = angular.module("UserFormApp", []);
    userFormApp.controller("UserFormCtr",
        function UserFormCtr($scope, $http) {
            $scope.submit = function () {
                var dataObj = {
                    FirstName: $scope.FirstName,
                    MiddleName: $scope.MiddleName
                };
                var res = $http.post("http://localhost:10160/UserService/AddUser", dataObj);
                res.success(function (data, status, headers, config) {

                    /****** Here I want to call the UsersController function of UserTableApp  ************/
                    /* I tried the code below but it did not work */
                    var scope = angular.element(document.getElementById("UserControllerDiv")).scope();
                    scope.$apply(function () { scope.UsersController($scope, $http); });
                    /*************************************************************************************/

                });
                res.error(function (data, status, headers, config) {
                    alert("failure message: " + JSON.stringify({ data: data }));
                });
            }
        });
    angular.bootstrap(document.getElementById("UserFormDiv"), ['UserFormApp']);
</script>

Any assistance provided would be highly valued...

Answer №1

Learn how to execute a function from another controller application with this simple guide.

 var scope = angular.element(document.getElementById("AnotherControllerDiv")).scope();
                scope.executeFunction();

Watch as the flow seamlessly transitions to the specified function. To confirm, verify that your method is present in the scope object within the console. You're on the right track!

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

troubles with variables in AngularJS Formly templates

Having trouble displaying model or other variables in my template? Check out this link for reference: http://jsbin.com/wofulinufo/edit?html,js,output. Question: What is the solution for displaying variables from controller? Even when I try using {{model} ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...

Implementing new updates to an object without affecting existing values in Vue.js 2

I am currently facing an issue with updating values for an object received in a payload before saving it to the database. Despite making changes, the new values are not being persisted correctly. I am utilizing Vue.js 2 for this task. How can I effectively ...

angular data binding returning the identifier instead of the content

I have been dealing with managed fields retrieved from a web server in the following format: { "fields":{ "relationshipStatus":[ { "fieldId":4, "name":"Committed" }, { "fieldId":2, ...

Guidance on retrieving state and city information using placeid within Google Maps API with AngularJS

Is there a way to retrieve the state and city information using a place_id? I am looking to extract the state and city based on the given place_id. You can refer to this link Google_map, where I need to find out the state and city of the specified place_i ...

Showing the date in AngularJSAngularJS can be used to

I have a view set up in AngularJS and I'm attempting to show the current date in a formatted way. My initial thought was to use <span>{{Date.now() | date:'yyyy-MM-dd'}}</span> to display the current date. ...

Using JavaScript, extract current date from an API data

Here is an example of how the data from my API appears: const data = [{ "id": "1", "name": "Lesley", "creationDate": "2019-11-21 20:33:49.04", }, { "id": "2", "name": "Claude", "creationDate": "2019-11-21 20:33:09.397", }, { "i ...

Receiving POST data in the req.body object with only the key specified in Express.js

I am encountering an issue with my current POST request. There is a simple function in place that handles the sending of data to the server using AJAX. handleSubmit(event) { var http = new XMLHttpRequest(); // object allows for making http requests // ...

Utilizing dynamic content to pass arguments to JavaScript functions

I'm facing a challenging issue that is causing me frustration. While browsing through this platform, I found some potential solutions but encountered a roadblock in implementing them successfully. My current project involves developing an e-commerce p ...

Exploring TypeORM: Leveraging the In() function within @ManyToMany relationships

There are two main characters in my story: Hero and Villain (their profiles are provided below). How can I utilize the Encounter() method to identify all instances where the Hero interacts with the Villain based on an array of Villain IDs? I am seeking a ...

scope.$digest completes before triggering scope.$watch in Karma unit tests

I am interested in testing this specific directive: .directive('uniqueDirective', function () { return { restrict: 'A', scope: { uniqueDirective: '@', tooltip: '@', placement: '@&apo ...

choosing a specific element with jQuery to be used for future purposes

I'm having some trouble with the following code snippet: var star = $("._container > favorite"); var symbol = $(star.parentNode.parentNode).attr("symbol"); var exchange = $(star.parentNode.parentNode).attr("exchange"); After running ...

Exploring the Integration of Node Modules with React

I am still learning about Node and I'm struggling to integrate an NPM package with my React project running on Node. Currently, I have a React component that successfully uploads a zip file through Node server scripts. Now, I am working on the next s ...

The comparison between AJAX and JSON passing and PHP generating HTML versus returning it

Currently, my code looks like this: <li onclick = " function CBAppData( callerObj, data ) { var string = ''; for( a in data ) { debug.push( data[ ...

Is it possible for a chrome extension to allow only specific third-party cookies and storage to be

My Chrome extension inserts an iframe from foo.com into bar.com, creating a situation where bar.com now includes a foo.com iframe. However, I have observed that this iframe encounters difficulties when attempting to write to localStorage if the user has ...

Transferring a single dataset from a table to a pop-up modal using Angular

I am working on a table to display entries for a contest, extracted from a database using PHP and converted into a .json object for AngularJS and JavaScript. I want to add a modal feature so that when a "judge" clicks on an entry, they can view its details ...

Unable to create a new collection in Firebase Firestore

I encountered an issue while trying to add a collection in Firebase Firestore using the function .collection(doc).set. Despite finding the new user in authentication in Firebase, the collection was not created and the console displayed an error message. ...

Automated pagination integrated with automatic refreshing of div every few seconds

I am currently working on setting up a datatable and I need it to be displayed on the monitor in such a way that it can refresh the div and automatically paginate to the next page. However, whenever the div is refreshed, the auto-pagination gets cancelle ...

I am looking to integrate my information into the user interface using Angular

import { Component, ViewEncapsulation } from '@angular/core'; import { Router } from '@angular/router'; import { Batch } from '../../../config/batchnew/batch.model'; import { BatchService } from '../../../config/batchnew ...

Navigating Angular's Credit Card Input Functionality

I am looking to limit the input capacity to 16 numbers and add a space between each set of 4 numbers. After conducting an extensive search for a credit card input that allows users to enter 16 digits with a " - " or space in between, all results were for ...