Error Message: Undefined Service in Angular version 1.5.4

I'm currently developing a sample application using AngularJS 1.5.4, built on angular seed, EcmaScript 6, and with a node.js web server.

For routing, I am following the guidelines provided here: https://docs.angularjs.org/guide/component-router. However, I encountered an error with a service when it is called from the component.

The specific error message reads:

etsy.controller.es6:17 Uncaught ReferenceError: EtsyService is not defined

Below are the relevant code snippets:

index.html

<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/bower-angular-router/angular1/angular_1_router.js"></script>
<script src="/bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
<script src="/app.js"></script>
<script src="/components/core/constants.es6"></script>
<script src="/components/etsy/etsy.service.es6"></script>
<script src="/components/etsy/etsy.controller.es6"></script>

etsy.service.es6

(function () {
'use strict'

// variables here ...

class EtsyService {
    constructor($http) {
        _http.set(this, $http);
    }

    // methods here ...

    static etsyServiceFactory($http) {
        return new EtsyService($http);
    }
}

angular
    .module('myApp.etsy')
    .factory('EtsyService', EtsyService.etsyServiceFactory);
})();

etsy.controller.es6

(function () {
'use strict';

class EtsyController {
    constructor($scope) {
        $scope.message = 'Hi from the $scope';
    }
}

angular
    .module('myApp.etsy', [])

    .service('etsyService', EtsyService)

    .component('etsy', {
        templateUrl: 'components/etsy/etsy.html',
        controller: EtsyController
    });
})();

I've been researching this issue extensively but I haven't been able to pinpoint what I might be doing incorrectly. Any suggestions or insights would be greatly appreciated.

Thank you

Answer №1

To successfully incorporate EtsyService into your angular application, it is essential to create a dedicated function within the etsy.controller.es6 file enclosed within an IIFE structure. For a clear example on how this can be achieved, refer to how the guide uses heroes.js and crisis.js. Check out this link for more insight: https://docs.angularjs.org/guide/component-router

Answer №2

If you make the calls in that sequence (servicer and controller), you will essentially be replacing the existing module by generating a new one:

angular
    .module('myApp.etsy')// <--------- obtaining an existing module
    .factory('EtsyService', EtsyService.etsyServiceFactory);

angular
    .module('myApp.etsy', [])//<--------------- creating a fresh module

    .service('etsyService', EtsyService)

Answer №3

If you rename the file to EtsyService.js, it should be able to run without any issues.

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

Issues with Three.js materials not rendering properly when loading .obj and .mtl files

I recently downloaded a free 3D model and I'm attempting to view it using three.js. Although the model is loading correctly, there seems to be an issue with the materials not loading properly. Strangely enough, only the wine bottles behind the bar are ...

Navigating through elements in the hidden shadow DOM

Can elements within the Shadow DOM be accessed using python-selenium? For example: There is an input field with type="date": <input type="date" name="bday"> I want to click on the date picker button located on the right and select a ...

Working with AngularJS: Utilizing the Ng-repeat directive for iterating

I am struggling to figure out why the following code is not functioning properly. <div ng-repeat="r in reservations" ng-init="new = r.some_code"> <div ng-if="old == new"> <p ng-init="sum = sum + r.cost">{{r.name}} - {{r.cost}}</p&g ...

Using angularJS factory to resolve with angular-ui-route

I am having trouble implementing the $stateProvider resolve feature in my Angular project. I have created a factory called playlistsService, but for some reason, the promise returned by the factory is empty. However, when I check the data from the $http ca ...

What steps can I take to bring this idea to life in my gallery?

Currently, I am facing a roadblock while transitioning my design concept into actual code. My main concern lies with creating this art piece. Although the gallery is up and running with all the images in place, I'm encountering difficulties with the s ...

Enhance your website with the jQuery autocomplete feature, complete with

Is there a way to incorporate smaller text descriptions alongside the search results displayed on my website? The descriptions are available in the data array used by autocomplete and can be accessed using the .result function by calling item.description. ...

Running a function on a specific route within the same file in Node.js - here's how to do it

I am looking to execute a function on a specific route that is defined within the same file. module.exports.controller = function(app) { app.get('/folders/create', createDirectory); } var createDirectory = function(path, name, permissions, v ...

Changing the prefix for a guild can be done without needing a complete restart, while adding a new guild to my database inexplicably requires one

I have set up a code that adds guilds to the database with default settings when they join. If the guild decides to change the prefix, it updates successfully and they can immediately start using it. However, there is an issue where I have to restart the b ...

What is the correct way to integrate the Ant Design library with Next.js for seamless server-side rendering?

I duplicated the official Next.js example using Ant Design at this link: https://github.com/vercel/next.js/tree/canary/examples/with-ant-design After cloning, I proceeded with npm install to install all dependencies. Then, I ran npm run dev to check if ev ...

I'm trying to create a horizontal list using ng-repeat but something isn't quite right. Can anyone help me figure out

Feeling a bit lost after staring at this code for what seems like an eternity. I'm trying to create a horizontal list of 2 image thumbnails within a modal using Angular's ng-repeat. Here's the HTML snippet: <div class="modal-body"> ...

Copy the contents of matrixA into matrixB and append a new element to each array within matrixB

I want to copy the values from matrixA into matrixB and then add a new element to each array in matrixB. let number = 100; matrixA = [ [1, 2], [3, 4] ]; matrixB = [ [1, 2, 100], [3, 4, 100] ]; Currently, my code looks like this: for (let ...

What's the best way to dynamically show Bootstrap collapse panels in a loop with AngularJS ng-repeat?

Currently, I am utilizing angularJS and attempting to include a bootstrap collapsible-panel within a loop. The code that I have written is causing all the panel bodies to be displayed beneath the first panel header. I need each body to be shown below i ...

What is the best way to access the text content of a nested HTML element for automation tasks with Selenium or Protractor?

Can anyone help me with this HTML code snippet? I want to extract and display only the text within the desc class - "Print this", ignoring the text in the spell class. This needs to be done using either Protractor or Selenium. <span class="desc"> Pr ...

Tips for sending an array of inputs to the query selector in Node.js using Cloudant

Currently, I am attempting to retrieve documents from a cloudant database using node.js. While I have successfully managed to obtain results for a single input value, I am encountering difficulty when it comes to querying with an array of inputs. For a si ...

I am looking to use flexbox and Vue.js to organize my sent messages in blue on the right and received messages in yellow on the left. How can I achieve this grouping effectively?

I've successfully built a messenger system using Vue.js with a Laravel backend. My goal is to display messages from myself (Jim, the logged in user) on the right side in blue and messages from Debbie (the recipient) on the left side in yellow, similar ...

Combine functions from two objects that share the same properties into an array for each property

I need help combining the methods from two objects into one, resulting in an array of methods for each property in the parent object: obj1 = {"prop1":"method1","prop2":"method2"} obj2 = {"prop1":"method3","prop2":"method4"} Expected result: obj1 = {"pro ...

Unable to access URLs manually

Each time I try to enter a url manually into my browser for an Angular-based website, I always encounter the following error: 'HTTP 404 The resource cannot be found Requested URL: /register' The only functional url is http://localhost:XXXX/in ...

Can I use MuiThemeProvider in my component without any restrictions?

I have a unique component called View: import React from 'react'; import { AppBar, Toolbar } from 'material-ui'; import { Typography } from 'material-ui'; import { MuiThemeProvider, createMuiTheme } from 'material-ui/st ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...

Error encountered while trying to upload a file using PHP on a hosting platform

Hey everyone, I'm feeling really frustrated right now because I'm facing a file upload error in my PHP code. The issue is that the file is not getting uploaded to my folder and no error messages are being displayed. I've been working on cr ...