How can you utilize functions from external files within Angularjs controllers that are not classified as controllers themselves?

I have a unique scenario that I need help with.

In my project, I am using a javascript file with three.js to render some models. To combine my frontend WebGL rendering with a backend library, I used Browsefiy to create a single js file named script.js.

Now, I want to trigger the init function in the script.js file from my controller in controllers.js. However, I am struggling to find a solution as the angularJs controller seems isolated from external code.

After combining the backend library, my rendering code expanded from 400 lines to over 2500 lines, making it difficult to manage in a single controller.

Is there a way to overcome this challenge? I would appreciate any suggestions or ideas!

<head>
<---These are the required angular files--->
<script src="./lib/angular/angular.min.js"></script>
<script src="./lib/angular-route/angular-route.min.js"></script>
<script src="./js/app.js"></script>    
<script src="./js/controllers.js"></script>
<script src="./js/services.js"></script>

<--These are the required rendering files--> 
<script src="lib/third-party/threejs/three.min.js"></script>
<script src="lib/third-party/threejs/StereoEffect.js"></script>
<script src="lib/third-party/threejs/DeviceOrientationControls.js"></script>
<script src="lib/third-party/threejs/OrbitControls.js"></script>
<script src="./js/script.js"></script>
</head>

Update I have shared my script.js file, which is quite lengthy. Any suggestions on how to access the init function easily would be appreciated. Thanks!

https://jsfiddle.net/ruiss/qr616n4q/

Answer №1

To establish a parent-child relationship in the controller, you can access it using

$scope.$parent.<anything>

If the relationship does not exist, you can add it to the $rootScope

Another option is to utilize the $broadcast and $emit methods to transmit data and listen for it to trigger a function (listener).

Alternatively, you can create services to store the function and share it among multiple controllers.

For more detailed information, refer to this link

EDIT:

Since the function is generic, you can explore these solutions-

Create IIFE -

var coreFunctions = (function() {
   return <Object_of_functions>;
} ());

If you are using ES6, you can do -

module.exports = {
    <function_to_export_1>,
    <function_to_export_2>,
    ....
};

You can also make the functions global by using window.<function>, but this may clutter the global scope and is not recommended.

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

Adjust the color of the sidebar's list items when scrolling

How can I change the background color of left sticky sidebars li on scrolling? When scrolling through BMW, BMW's background color in the sidebar should turn green. Please refer to the code snippet provided below. The background color of the li ...

Is it possible to personalize/modify the dropdown menu text?

Is it feasible to adjust the positioning and font color of my drop-down menu text? I aim to have the "title" section in black text on the left side and the "type" section in gray text on the right side of the drop-down menu. Currently, all the text is in ...

Angular Boilerplate is experiencing difficulties in properly reading ABP

Working on my boilerplate project, I am now diving into consuming backend services (using asp .net) in Angular through http requests. However, I encountered an issue when trying to implement the delete method in mycomponent.ts, as TypeScript was not recogn ...

Dynamically remove values from other fields in a React Formik form based on checkbox conditions

Currently, I am utilizing react-formik for my form implementation. I have encountered an issue with checkbox-based conditions where the checked status of a checkbox determines whether two hidden fields are displayed or hidden. If the user checks the checkb ...

Navigating to a parent URL where the Angular configuration is set can be achieved by following these steps

My application revolves around a webpage created with the use of node and angular. On the root URL (/), I have incorporated a signup and login form without the use of Angular. Upon successful login, Angular scripts and configuration files are loaded on the ...

Pair of Javascript Functions Using Async with Parameters

This question builds upon a previous inquiry raised on Stack Overflow: When considering approach number 3 (referred to as the "counter" method), how can we ensure that the function handleCompletion can access necessary data from startOtherAsync to perform ...

best way to display an array in HTML

When I input a manufacturer, I want to display the complete data from each object inside the array in the HTML. However, when I call the function on the HTML page, all I get back is the word object repeated as many times as the Manufacturer is defined in ...

Initiating PHP outcomes with the integration of JQUERY and Bootstrap

Currently, I am working on a Twitter search functionality that allows me to search for any content on Twitter. While the feature is functional, I am facing some challenges with displaying the results in the desired format. Ideally, I would like the results ...

Node.js app experiencing intermittent issues with AngularJS interceptor failing to include JWT Bearer token in all requests

I have implemented a JWT authentication system in a node app. To ensure the Bearer token is included in every request, I used an interceptor. It functions correctly when accessing a restricted route within Angular or by using curl with the token specified ...

Is there a versatile Node.js HTTP request module that is compatible with both server-side and browser-side development, particularly when packaged with Webpack?

Currently, I am searching for a request module that can operate seamlessly in both the Node.js server and the client when compiled with Webpack. The requirements are quite straightforward. I simply need to execute some basic HTTP Ajax requests such as get ...

Seeking assistance with coding a beginner-level Google Chrome extension

Currently, I am working on developing a basic Google Chrome extension with 2 or 3 browser actions. I have been using Selenium IDE to capture the necessary steps in Firefox that I need for my project. However, I am unsure of how to translate these recorde ...

The Angular route seems to be unresponsive to a single click, but it starts functioning properly when clicked

I am utilizing a data service to handle all my asynchronous data operations. Whenever I click the ERASE button, a function is triggered to erase all data and return an object indicating the operation status (Success: true/false). If the value returned is ...

Simple method for converting pixel values to em units

I'm searching for a simple method to incorporate a line of code into one of my plugins in order to convert specific pixel values into em values. My project's layout requires the use of ems, and I'd prefer not to rely on external plugins for ...

"Implementing an infinite scroll feature using AJAX and PHP to dynamically load

Currently, I am in the process of loading a significant number of products on a single page and have decided to use an OnScroll loading approach. The following is the method that I am implementing: <ul id="productContainer"> </ul> JQuery: $ ...

Issue encountered while fetching data from SQL database in a Node.js application

After successfully connecting the database to my node js project, I encountered an issue when attempting to retrieve data using the get operation. Despite the connection working fine, I was unable to resolve the error that occurred. Establishing the SQL c ...

I am attempting to trigger a mouseup event following a mousedown action

elements[0].onmousedown = function(){ console.log('screen clicked.'); triggerMouseUp(); }; I just need to incorporate a function in my code that simulates mouseup event, even when the user is still holding down the click button. e ...

Using AngularJS: Implementing asynchronous $http.jsonp request through a service

I am currently developing a straightforward application that involves the following steps: 1. The user provides 2 parameters and clicks a button 2. Angular communicates with an external JAVA Servlet that sends back JSON data 3. The application displays the ...

Send the value to the <input> tag following each iteration in the functions

I am currently utilizing BootstrapVue. In my code, I have implemented a for loop to retrieve unique numbers from an array, which are stored as this.number. For each iteration of the loop, I use input.push() to add a new b-form-input element (in this case, ...

What could be causing the Universal Code for Disqus to not function properly?

Struggling to get this code to work on my website. I've tried everything, from clearing caches and cookies to disabling plugins and extensions, but still no luck. Check out the code below: import React, { Component } from 'react' import { ...

AdjustIframeHeightOnLoad is undefined. Please define it before use

I've noticed that a few of my website pages are loading very slowly. After checking Google inspect (console), it seems like the issue is caused by this error: Uncaught ReferenceError: AdjustIframeHeightOnLoad is not defined. This specific piece of co ...