Access a factory from a separate module in AngularJS

Chapter 1:

angular.module('chapter1', [])
   .factory('chapter1Fac', function(){
        return {
          display: function (){
              return('This message is from chapter 1');
          }
        };
   };

Chapter 2:

angular.module('chapter2', ['chapter1'])
   .controller('chapter2Ctrl', function (chapter1Fac, $scope){
       $scope.output = chapter1Fac.display();
   }

What modifications do I need to make in this code to access the information from chapter 1's factory in chapter 2's controller?

Answer â„–1

To make it work, simply inject $scope into your code:

angular.module('app', ['service'])
   .controller('mainCtrl', function (serviceFactory,$scope){
       $scope.data = serviceFactory.getData();
   }

Check out the live example on Example

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

Unable to submit CSV file for Controller Action

I am encountering an issue with uploading a csv file to my backend action method. I have an action method called UploadPropertyCSV in Controller PropertyController that is supposed to process the file and add it to the database. However, when I click submi ...

Sending data from a controller to an AngularJS app config

I'm fairly new to working with Angular. Within my Angular application, I have implemented token authentication. To handle situations where my backend returns a 401 error, I am using an HTTP interceptor that triggers a login process using a refresh tok ...

Including a hyperlink in VUE Bootstrap for seamless user navigation

Why does this always drive me crazy? I'm determined to include an external link () and an image(enter image description here) on my portfolio page within the title of the project. main.js { id: 18, url: 'single-portfolio. ...

Is there a way to eliminate the impact of Jquery Document Click Listeners on a React Element?

We are currently in the process of rewriting parts of our legacy web app using React incrementally. As a result, we are unable to completely eliminate various document listeners that are scattered throughout the code. The presence of these listeners on dif ...

Implementing JavaScript to override inline CSS styles

Is it possible to override inline CSS using JavaScript with compatibility for IE6? I came across a pure CSS solution, but unfortunately it doesn't work in IE. http://www.sitepoint.com/blogs/2009/05/27/override-inline-css/ <div class="block"> ...

There is a runtime error in Microsoft JScript, as the object does not support the property or method '__defineGetter__'

Upon opening my project in IE9, I encountered the error message: "Microsoft JScript runtime error: Object doesn't support property or method 'defineGetter'." Can anyone provide guidance on how to resolve this issue? ...

How to Generate a JSON Cookie Array?

Currently, I am in the process of leveraging jquery's json to form a cookie array. The script I have managed to put together thus far is functional except for the part pertaining to the array. Would someone be able to guide me on how to construct an a ...

Assigning a value to a variable from a method in Vue: a step-by-step guide

I'm having trouble assigning values from a method to variables in HTML. Here's what I have in my code: <b-card-text>X position {{xpos}}</b-card-text> <b-card-text>Y position {{ypos}}</b-card-text> I would like to assign v ...

Encountering a syntax error while attempting to send Node.js data to MySQL database

I am facing a problem with an SQL error that I just can't seem to figure out. Here is the error message: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual for MySQL server version for correct syntax near 'x Disney â ...

What are the benefits of using `observer` over `inject` when passing data to a React component in MobX?

After reading MobX documentation, it appears that using observer on all components is recommended. However, I have discovered that by utilizing the inject method, I am able to achieve more precise control over which data triggers a re-render of my componen ...

What could be causing the issue of opacity and list reordering not functioning properly in react-dnd and react-virtualization?

I'm currently setting up a react-dnd sortable list within a react-virtualized List. I've been referencing an example from react-dnd which can be found here. Most of it is working as expected, but I'm encountering an issue where the items I ...

Automate the process of making Tailwind Classes non-Global with ease!

I am currently working on an application that consists of two main parts: (X) an older AngularJs legacy application with its own set of CSS classes. (Y) a new React application contained within a div. This new part (Y) is built using webpack, postcss, and ...

How does jquery's removeClass function on a button continue to function smoothly even after an ajax call

Code Snippet: <span class="input-group-text"> <button type="button" class="btn btn-outline-danger btn-sm owner_button owner_check" name="owner_check" > <i class="far fa-check-circle"></i> </button> </span> ...

Tips for incorporating in/out animations with a container for the Slide feature:

I need help creating a component that will slide to the right when mounted, and to the left when unmounted. The Slide component should be contained in a div with the class "profile-slide-container", but I'm unsure how to achieve this. Below is the co ...

The expiration and creation of access tokens in the Gmail API

For the past 6 months, I have been utilizing the Google API to send emails from my server in a Node.js project. I have successfully set up credentials and generated both refresh and access tokens, which I have been using consistently. oAuth2Client = new go ...

Using varying data types in a byte array with Node.js

I am facing a challenge where I need to create a byte array containing different data types. For instance, the array will include Byte (0-100), Byte (0-10), Two bytes (-30 to +100), Bool (0/1), Byte, and Two Bytes (0-300). The client will receive this byt ...

Using a PHP variable within a JavaScript function to incorporate its value into a select field, ultimately generating a response using JavaScript

As I work on developing a shopping cart, I have successfully stored the total value of the items in the cart in a PHP variable named $total. I attempted to transfer this value into a JavaScript variable labeled Shipping fee. Subsequently, I created a form ...

Concealing a div based on a condition

Having difficulty concealing a div once a specific condition is met. The condition depends on the user logging into a web application. In my CSS file, I have set the multipleBox div to visibility: hidden, along with other styling attributes like border co ...

Receive emails: using the require() function with ES Module

After following the react-email documentation, I encountered an error when trying to run the yarn email command: $ email dev --port 3001 ***\node_modules\ora\index.js:65 if (process.platform === 'win32') { ...

Construct a new array within a JavaScript constructor function

I have been working on setting up a constructor and trying to initialize an array inside the object that will be created. This specific array is meant to hold multiple objects. function Cluster3DObject(name){ this.name = name; ...