Angelic: peculiar actions with ngCookies

Within my Angular application

var mainApp = angular.module('mainApp', ['ngCookies']);

I have created the authCtrl controller:

mainApp.controller('authCtrl', ['$scope, $cookies',function ($scope, $http, $cookies) {

    $scope.credentials = {};

    $scope.signCheck = function () {
        a = $cookies.getObject('session_credentials');
        console.log(a);
    };
}]);

When I remove the $scope declaration from the array of injections:

mainApp.controller('authCtrl', ['$cookies',function ($scope, $http, $cookies) {

$scope becomes undefined. If I omit $cookies, then it also becomes undefined. And if both are kept, an unknown provider error is thrown by $injector.

What mistake am I making?

Answer №1

To ensure proper functionality, remember to arrange the services correctly in the injector array and controller function parameters:

The Angular documentation states:

This is the recommended method for annotating application components. It is the standard practice demonstrated in the documentation.

For instance:

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);

In this example, we provide an array containing a series of strings (dependencies' names) followed by the actual function.

When utilizing this form of annotation, it is important to maintain consistency among the annotation array and function parameters.

The following controller definition may be suitable for your needs:

mainApp.controller('authCtrl', ['$scope', '$http', '$cookies', function ($scope, $http, $cookies) {

    $scope.credentials = {};

    $scope.signCheck = function () {
        a = $cookies.getObject('session_credentials');
        console.log(a);
    };
}]);

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

Tips for establishing secure communication between a shell app and micro application (frontend) using pubsub technology

I have developed a shell application that serves as the main container for handling all API communications. Additionally, I have created several Micro applications that simply send API request signals to the shell application. When it comes to security, m ...

React and Express working seamlessly together on separate localhost ports without CORS interfering with the fetch call

I'm puzzled by why CORS isn't blocking the Axios get request. Here are the specifics: React running on: localhost:3000 Express running on: localhost:3001 Express code snippet: const express = require('express'); const app = express ...

The React.js Redux reducer fails to add the item to the state

I'm just starting to learn about React.js and Redux. Currently, I am working on creating a basic shopping cart application. https://i.stack.imgur.com/BfvMY.png My goal is to have an item (such as a banana) added to the cart when clicked. (This sho ...

Is there a way to streamline this function call that appears to be redundantly repeating the same actions?

I have developed a function to search for blog posts, prioritizing titles over excerpts and excerpts over content when added to the containsQuery array. While the code seems to be working well, I have noticed that there is a lot of redundant code. How can ...

Node.js is not accurately setting the content length. How can I resolve this issue?

I could use some assistance. Even though I have set the content-length on the response object, it doesn't seem to be working. Have I done something incorrectly? res.set({ 'Content-Type': res._data.ContentType, 'Content-Length' ...

What is the best way to assign a JavaScript string to a Java string?

This particular dropdown list allows for multiple values to be selected -- A loop has been utilized to populate the options in the list by fetching data from a database. Here is the javascript function I am attempting to implement in order to retrieve th ...

Creating a persistent sticky element that remains at the forefront in Electron or JavaScript development

Currently, I have a remote desktop control application built on Electron that functions as a desktop app on Mac and Windows, and as a web app on Chrome and Firefox. My next goal is to create a sticky component that remains on top at all times, regardless ...

Combining information once constructing a complex array structure in JavaScript

My reduce function is successfully building multiple levels of data, but I have encountered an issue. Currently, it organizes the data by employee first, then by date, area, and job. While all the data is displayed correctly at each level, I am now attempt ...

What is the best way to change the class of the inline table of contents element using JavaScript?

INQUIRY Hello, I am currently working on building a straightforward navigation site and I have a question regarding how to modify or add a class to one of the li elements within the inline table of contents (toc) on the right side. I want to style it usin ...

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

An unforeseen repetition of jQuery Ajax calls

I'm currently working on an application that utilizes ajax calls to load content. However, I've encountered a situation where an ajax call goes into a loop but seems to end randomly. Below is the code sequence starting from a double click event l ...

My variable from subscribe is inaccessible to Angular2's NgIf

My goal is to display a spinner on my application while the data is being loaded. To achieve this, I set a variable named IsBusy to True before making the service call, and once the call is complete, I set IsBusy to false. However, I am facing an issue wh ...

Attempting to save data to a .txt file using PHP and making an AJAX POST request

I have been facing an issue while trying to save a dynamically created string based on user interaction in my web app. It's just a simple string without anything special. I am using ajax to send this string to the server, and although it reaches the f ...

Creating custom services that modify or extend default services in a shared module

I am facing a dilemma with my shared module in AngularJS 1.6.5. This module is designed to be used across multiple applications, each requiring different services within the module to be overridden to cater to their specific needs. These variations are mai ...

Laravel's blade allows you to easily convert an HTML element to an array using the same name

I have two separate divs with similar content but different values <div id="tracks-1"> <div> <label>Song Title</label> <input type="text" name="tracks[song_title]" value=""> <input type="text ...

The jQuery Bootstrap extension functions correctly on Firefox, but encounters compatibility issues on IE10

My code is based on jQuery 2.1 and bootstrap 3.2. It runs smoothly in Firefox, but encounters issues in IE10. I have added <meta http-equiv="X-UA-Compatible" content="IE=edge"> to the header to address compatibility mode concerns, yet the problem per ...

How can I detect the @mouseup event in a Vue Bootstrap slider?

I have been using a vue-bootstrap slider that comes with two actions, @change and @update. My goal is to capture the final position of the slider once the movement ends. To achieve this, I decided to test both actions. In my scenario, @change triggers ev ...

The form's onsubmit function, which triggers an ajax call, does not complete before the form proceeds to its next action. Interestingly, this issue is only experienced on

In my Django application, I am developing a feature that allows users to create rubrics for grading assignments. Each rubric item can be edited, and upon form submission, the system checks if any rubric items have been modified. If changes are detected, th ...

Is there a method to reduce the requirement for if-conditions in this situation?

After revisiting this previous inquiry, I successfully implemented multiple filters on my observable, based on the user's chosen filters. However, the issue arises from the uncertainty of whether a filter is applied and the varying behavior of each f ...

What could be the reason for this Javascript code not functioning as intended, failing to generate a random number between 1 and 3 when I click on any of the buttons

Could someone help me with generating a random number between 1 and 3 each time I click on one of the buttons (rock, paper, scissors)? I am new to programming and not sure what I'm doing wrong. <!doctype html> <html lang="en"> <head& ...