Executing one controller function from another controller in Typescript

There are two typescript controllers in my project

Controller A
{
   public methodOfA()
   {//perform some action}
}

Controller B
{
   public methodOfB()
   {//perform some action}
}

I am trying to implement the following functionality

Controller B
{
   A.methodOfA();
}

Answer №1

Perhaps a potential solution could look like the code snippet below:

class ManagerX
{
    constructor($scope)
    {
        //perform certain actions
    }

    public static executeTask()
    {
        //carry out specific task
    }
}

class ManagerY
{
    constructor($scope)
    {
        //take necessary steps
        ManagerX.executeTask();
    }
}

It is worth noting, as mentioned by others in the comments, that it may be advisable to utilize services instead of this approach.

Answer №2

     class ControllerA {
           static $inject = ['$http', '$scope','app.services.CommonService'];
           constructor(
                private $http: ng.IHttpService,
                private $scope: ng.IScope, 
                private commonService: app.services.ICommonService){

            }
         //calling a common method from controller A
         this.commonService.CommonMethod();
        }

     class ControllerB {
            static $inject = ['$http', '$scope','app.services.CommonService'];
            constructor(
                private $http: ng.IHttpService,
                private $scope: ng.IScope, 
                private commonService: app.services.ICommonService){

            }
         //executing a common method from controller B
         this.commonService.CommonMethod();
       }

 module app.services {   
     //common interface for making service accessible
     export interface ICommonService {
         CommonMethod();
     }

     export class CommonService implements ICommonService{
            static $inject = ['$http'];
            constructor(private $http: ng.IHttpService) {

            }

            public CommonMethod(){
                //implementation of a common method here
            }
     }

     factory.$inject = ['$http'];
     function factory($http: ng.IHttpService) {
        return new CommonService($http);
     }

     angular.module('yourApp')
     .factory('app.services.CommonService', factory);
}

Using a shared service to expose commonly used methods was suggested by Kobi.

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

Construct a string by combining the elements of a multi-dimensional array of children, organized into grouped

My task involves manipulating a complex, deeply nested array of nodes to create a specific query string structure. The desired format for the query string is as follows: (FULL_NAME="x" AND NOT(AGE="30" OR AGE="40" AND (ADDRESS ...

Issues occur during installation of Angular on Mac Catalina, encountering errors while trying to run the installation command for Angular: `% sudo npm

My npm version is 6.14.6 and node version is v12.18.3. I have attempted the following: Added sudo in the beginning, but still not working. Tried to install har-validator using command: sudo npm install har-validator Attempted: npm install --force expo-cli ...

Is there a way to insert rows and columns into the table headers using React Material UI?

I am new to working with material UI and I am having trouble figuring out how to add columns in the table header. The image below shows what I am trying to achieve - under "Economics", there should be 3 columns, each of which will then have two more column ...

A more efficient method for defining and utilizing string enums in Typescript

enum PAGES { HOME = 'HOME', CONTACT = 'CONTACT', } export const links: { [key: string]: string } = { [PAGES.HOME]: '/home', [PAGES.CONTACT]: '/contact', }; export function getLink(page: string) { return B ...

The call to the hooks is not valid. Hooks must be called within the body of a functional component

Could you please take a moment to review the validate method within the elfe-if condition in the code snippet below? I am encountering an issue when trying to invoke the useLocation method from react-router-dom. Upon researching online, I came across simil ...

Positional vs Named Parameters in TypeScript Constructor

Currently, I have a class that requires 7+ positional parameters. class User { constructor (param1, param2, param3, …etc) { // … } } I am looking to switch to named parameters using an options object. type UserOptions = { param1: string // ...

Unable to retrieve array data using the post method in PHP and Angular.js

I am encountering a problem with my form submission using Angular.js and PHP. Despite sending an array of data to the server side using the $http service, I am not receiving any values on the server side. Below is an explanation of my code. var supplierDa ...

Utilize array mapping to alter the complete object

In my project using TypeScript (Angular 2), I am working on creating a "reset" method for an object array: cars [ { id: 1, color: white, brand: Ford, model: Mustang, ... }, ... ] Users have the ability to modify these objects, ...

Angular Translate Text Selection Using Protractor

I am currently using protractor and need to perform a .click() action on a button that is selected by.partialLinkText. We are utilizing angular-translate in the following manner: a(href="/documents/Impressum.pdf", target="_blank", ng-click="closePanelByIm ...

Sending a CSS class to an Angular library

In my development process, I am currently working on creating a library using Angular CDK specifically for custom modals. One feature I want to implement is the ability for applications using the library to pass a CSS class name along with other modal conf ...

Expanding the Mui Typescript breakpoints within a TypeScript environment

Encountering a typescript error when attempting to utilize custom names for breakpoint values: Type '{ mobile: number; tablet: number; desktop: number;}' is not compatible with type '{ xs: number; sm: number; md: number; lg: number; xl: numb ...

An excellent server-side resolution for the Angular.js

Currently, I am utilizing Angular.js for my project. The core logic of the business is developed within Angular and there are two specific areas I really need assistance with: I am in search of a seamless method to authenticate users, but I'm unce ...

Integrating external JavaScript libraries into Ionic using the correct process

For the last few months, I have been utilizing jQuery Mobile for a hybrid app. Now, I am interested in exploring Ionic and Angular.js, so I am attempting to reconstruct it. My current JQM application relies on xml2json.js, but I do not have any experience ...

Display div content depending on the clicked ID in AngularJS

Unique Header Links HTML <ul ng-controller="pageRouteCtrl"> <li ui-sref-active="active"> <a ui-sref="home" class="" ng-click="getPageId('live-view')">LIVE</a> </li> <li> <a ng-clic ...

Using AngularJS ng-repeat to pass href links

I am facing an issue with displaying hyperlinks in a list of messages obtained from server response using AngularJS. $scope.messages = [] When a button is clicked, the content of a text area is added to the array using ng-click method. This message is th ...

AngularJS is patiently awaiting the completion of the translation rendering process

I implemented the ngCloak directive to my body element on the page and every angular-related element, but it appears that it is not working with AngularJS translate. The translate variables show up on the page initially and then get translated after a se ...

Concealing the sidebar in React with the help of Ant Design

I want to create a sidebar that can be hidden by clicking an icon in the navigation bar without using classes. Although I may be approaching this incorrectly, I prefer to keep it simple. The error message I encountered is: (property) collapsed: boolean ...

How can you assign various models to a single directive?

Exploring angularjs, I am interested in creating a dual list box with two lists - one on the left containing all items, and another on the right for selected items that can be transferred back and forth. Using arrows to facilitate this movement. While it ...

When choosing an item in the typeahead feature, the selected value is empty

I'm currently integrating typeahead into an existing project, so I assume the issue lies within Below is my code snippet: <pre>Model: {{entry.value | json}}</pre> <input ng-show="entry.isToShow" class="form-control col-xs-12 " ng-mo ...

What steps should be taken to properly assess an AngularJS provider setup?

My provider definition looks like this: (function(angular) { angular.module('myModule', []) .provider('myService', function () { var service = {}; service.configureSomething = function () { }; service.$get = function () { ...