Utilizing Angular Dependency Injection to Share Data Among Modules Through a Service

After reading about dependency injection in Angular, I decided to test it out. However, when trying to inject the module, I encountered an issue with passing values from one module to another and outputting them on the console.

(Update: The issue has been resolved, but now I'm getting an error - Uncaught Error: No module: sisUser angular.min.js:18. It's not even evaluating the angular expression {{2+2}})

Let me walk you through the scenario:

Login.html

A simple Login page that asks for two text inputs - username and password, which are stored in a service called "user".

<html lang="en" ng-app="wbsislogin">
<head>
</head>
<body>
<form>
 <fieldset>
    <label>
      <span class="block input-icon input-icon-right">
         <input type="text" class="span12" ng-model="username" placeholder="Username" />
         <i class="icon-user"></i>
        </span>
    </label>

    <label>
      <span class="block input-icon input-icon-right">
          <input type="password" class="span12" ng-model="password" placeholder="Password" />
          <i class="icon-lock"></i>
      </span>
    </label>

    <div class="space"></div>

    <div class="clearfix">
    <label class="inline">
    <input type="checkbox" class="ace" />
        <span class="lbl"> Remember Me</span>
    </label>

    <button ng-click="loginbtn()" class="width-35 pull-right btn btn-small btn-primary">
       <i class="icon-key"></i>
       Login
    </button>
    </div>

    <div class="space-4"></div>
    </fieldset>
</form>
<script src="angular/angular.min.js"></script>
    <script src="wbsis.js"></script>
</body>
</html>

Dash.html

This page simply displays the passed username and password from Login.html using the injected service.

<html lang="en" ng-app="wbsislogin">
<head>
</head>
<body>
<p>{{2+2}}</p>
<div ng-controller="ditest">
    {{ usen }} {{ pasw }}
</div>

<script src="angular/angular.min.js"></script>
    <script src="wbsis.js"></script>
</body>
</html>

wbsis.js

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["wbsislogin"]); // according to solution 1

sisUser.controller("loginformCtrl",function($scope,user,$log,$location){
$scope.username = "s";
$scope.password = "test2";
$scope.loginbtn = function(){
    user.usern = $scope.username;
    user.passw = $scope.password;
    $log.log(user.usern,user.passw);
    window.location.href='http://sis_frnt.dev/app/dash.html';

}

});

sisUser.factory("user", [function($log){
var user = {
    usern: '',
    passw: ''
};
return user;
}]);
wbSis.controller("ditest", function($scope, $log, user){
$log.log(user.usern,user.passw);
$scope.usen = user.usern;
$scope.pasw = user.passw;

})

Please help me pinpoint where I may be going wrong in this setup.

Answer №1

Make sure to include the module name as a dependency instead of just the variable name.

Update your code like this:

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["sisUser"]);

Change it to:

var sisUser = angular.module("wbsislogin",[]);
var wbSis = angular.module("wbsis",["wbsislogin"]);

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

"Troubleshooting issue with jQuery failing to identify PHP variable within an if

I have managed to implement a code that utilizes jQuery and PHP session to add items to the cart. Everything is working smoothly except for displaying the status of the action, such as "Added to cart" or "Updated". The message about the status is stored in ...

Axio GET request in VueJS does not return a response body when using Amazon API Gateway

UPDATE: While Postman and browsers are able to receive valid response bodies from an Amazon API Gateway endpoint, other web applications are not. This is a basic GET request with no headers, and no authentication is needed for the API endpoint. The data is ...

Error message displayed when attempting to send emails through an HTML form obtained from a complimentary template site

Currently, I am in the process of learning to code basic websites but I'm facing some challenges with getting the contact form to function properly. I decided to work with a free template that can be found at this link: and uploaded it to a shared ho ...

Changing Ionic back button to a specific route/view

Coming from view1, I navigate to $state.go('app.view3'); Now that I'm in my view3.html, how can I configure the back button to direct to view2 when clicked by the user? ...

Ways of transferring ASP.NET MVC view data to an AngularJS controller

I am looking for a way to transfer values from an ASP.NET MVC view (.cshtml) to an AngularJS controller. While I am proficient in AngularJS, I need guidance on how to achieve this in MVC. I have values stored in the MVC cshtml file that I would like to pas ...

Problem with the show/hide feature on jQuery. Automatically scrolls to the beginning of the page

On my website, I have successfully implemented two basic Show / Hide links that are working great. Here is the HTML code: <!DOCTYPE html> <html lang="en"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type" conte ...

Is there a way to align these side by side?

Is it a silly question? Perhaps. But I can't seem to figure out how to align my text and colorpicker on the same line instead of two. Take a look at my fiddle. I've tried removing display:block and clear:both, but that didn't do the trick. H ...

Verify if the term is present in an external JSON file

I am currently using tag-it to allow users to create tags for their posts. At the moment, users can type any word, but I have a list of prohibited words stored in JSON format. I am looking for a way to integrate this list into the tagit plugin so that if ...

Placeholder fails to appear

After implementing some jQuery validation, I wanted to display a text as a placeholder when the user skipped out of an input field without entering anything. This is the code I wrote: $('input[type="text"]').on('blur', function() { ...

Versatile route able to handle any request thrown its way

My Node.js app is up and running smoothly using the Express.js framework. All routes in my application are set to post routes, such as: app.post('/login', function(req, res){ //perform login validation }); app.post('/AppHomepage', fun ...

Is there a way to transfer a JSON object to Excel using Nextjs/React?

How can I create a button that exports data from a JSON object to multiple file formats, including Excel (.xlsx)? "data": [ { "id": 1, "temaIndicador": "Indian", "codigo": "001", "observacion ...

Error message in ThreeJS KTX2Loader: "Failed to load incompatible compressed texture format in .uploadTexture()"

While troubleshooting unfamiliar code, I encountered the following 3 errors related to loading KTX2 textures: THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture() WebGL warning: compressedTexSubImage: format must ...

Vue 3 Router view fails to capture child's event

After some testing, I discovered that the router-view component in Vue 3 does not capture events sent from its child components. An example of this scenario is as follows: <router-view @event-test="$emit('new-test-event')" /& ...

What methods can I use to enable web browsers to remember form field data that is not transmitted in the usual manner?

The code provided here is almost perfect in terms of functionality. function post_form() { http=new XMLHttpRequest(); http.onreadystatechange=function() { if (http.readyState==4 && http.status==200) some_div.innerHTML=http.responseText; ...

Display additional tiles within a compact container

I'm attempting to replicate the user interface used by foursquare! Positioning the map slightly off center like they have . I've figured out how to do one part but struggling with the second part. Initially, I loaded the map in a small div (t ...

I seem to be having trouble with jQuery. Every time I try to submit my form, nothing happens

I am currently working on creating a web page for a numerical game, but I am facing difficulties with my jQuery implementation. Despite checking all my placements, nothing seems to happen when I click on the Submit button. Any assistance would be greatly a ...

Slerping with quaternions in Three.js can produce undesirable outcomes when near the poles

Check out this video example: https://drive.google.com/file/d/18Ep4i1JMs7QvW9m-3U4oyQ4sM0CfIFzP/view In the demonstration, I showcase how I determine the world position of a ray hitting a globe under the mouse cursor. By utilizing lookAt() with a THREE.Gr ...

What is the best way to pass data from the server to a Vue CLI application?

I am looking for a way to connect my Vue CLI app to a server back-end. The server will send a view template along with data payload, and I need to inject that payload as JSON into the Vue app as a data property. Here is a snippet of the index.html file ge ...

Performing a mass update in MongoDB with the help of mongoose

Is there a way to perform bulk upserts with Mongoose? Essentially, I want to have an array and insert each element if it does not exist, or update it if it does. (I am using custom _ids). When I try using .insert, MongoDB throws an error E11000 for duplic ...

Exploring the power of Reactjs and managing server-side data

As a newcomer to the world of Javascript frameworks, I am on the lookout for the perfect framework for my upcoming projects. Up until now, I have been developing simple applications using the MVC framework Django with Bootstrap for the frontend. I apprecia ...