The constructor in my Controller never seems to be invoked (AngularJS)

Currently, I am in the process of setting up a small angularjs application within a Sharepoint web part. Although this is my first time attempting this task, I managed to create a demo that worked within this environment before diving into the actual code. However, I have encountered an issue where it seems like everything is correctly connected, yet the Controller constructor never gets called. Despite knowing that the code is incomplete, the constructor should at least be accessed in order to call the service. The only alerts I am seeing are the ones present in the App file. Below are the various files involved:

HTML:

<div id="bidragsoplysninger2015App">
<div data-ng-controller="bidragsoplysninger2015Controller">
    <div id="template-content" ng-include="'/AngularJS/Views/Bidragsoplysninger2015.html'"></div>
</div>

Partial:

<ul>
    <li data-ng-repeat="indbetaling in indbetalinger | orderBy:'registreringsdato'">{{indbetaling.registreringsdato}} - {{indbetaling.beloeb}}</li>
</ul>

Model:

class Indbetaling {
constructor(
    public beloeb: number,
    public betalingsform: string,
    public fagkode: string,
    public indskudstype: string,
    public institutionsNr: string,
    public kundeNr: number,
    public ordningNr: number,
    public pensionsform: string,
    public pensionsmodel: string,
    public registreringsdato: Date,
    public valoerMaaned: string
) {}

}

Interface:

interface IIndbetalinger {
getIndbetalinger(cpr: string): Indbetaling[]

}

Service:

class Bidragsoplysninger2015Service implements IIndbetalinger
{
private scope: any;
private http: any;

constructor($scope: ng.IScope, $http: ng.IHttpService) {
    this.scope = $scope;
    this.http = $http;
}

getIndbetalinger(cpr: string): Indbetaling[]
{
    var promise = this.http.get('/_layouts/wpgenerelportal/Indbetalinger.aspx/GetIndbetalinger?cpr=' + cpr);
    promise = promise.then(this.mapIndbetalinger).then(response => response.data);
    return promise;
}

mapIndbetalinger(data) {
    alert("Mapping!");
}

}

Controller:

class Bidragsoplysninger2015Controller {
private indbetalinger: Indbetaling[];

public static $inject = [
    '$scope',
    'bidragsoplysninger2015Service'
];

constructor(private $scope, private bidragsoplysninger2015Service: IIndbetalinger) {
    alert("Controller constructor");
    this.indbetalinger = $scope.indbetalinger = bidragsoplysninger2015Service.getIndbetalinger("110680-3419");

    // 'vm' stands for 'view model'. We're adding a reference to the controller to the scope
    // for its methods to be accessible from view / HTML
    $scope.vm = this;
}

}

App:

alert("In app 1");

var bidragsoplysninger2015AppModule = angular.module('bidragsoplysninger2015App', []); 

bidragsoplysninger2015AppModule.service("bidragsoplysninger2015Service", Bidragsoplysninger2015Service);
bidragsoplysninger2015AppModule.controller("bidragsoplysninger2015Controller", Bidragsoplysninger2015Controller);

alert("In app 2");

$(document).ready(() => {
    alert("In app 3");
    angular.bootstrap($("#bidragsoplysninger2015App"), ['bidragsoplysninger2015App']);
});

Answer №1

After some digging, I finally uncovered the issue in this particular post: 'unknown provider error' in Angular app

The crucial realization came when I read the sentence: "Services don't have $scope." Removing $scope as an input parameter to the service constructor resolved the problem.

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

generate iframes on secure websites

I'm in the process of developing a bookmarklet. Essentially, it retrieves a snippet of JavaScript code from the server and generates an iframe on websites with different domains. While it operates smoothly on http websites, it appears to be restricte ...

What happens when you click and trigger mouseleave?

window.onload = function() { $(".compartir").hover(function() { console.log('hover'); var self = this; setTimeout($(self).addClass('ready'), 500); }, function() { var self = this; console.log('leave'); ...

Enabling AngularJS to Communicate with PHP by Setting the Access-Control-Allow

In my current setup, I have two web servers - one local (Apache) and one remote (Lighttpd). The local webserver hosts a web app where I need to send an ajax request to the remote server using AngularJS $http. var req = { method: 'POST', ...

Loop through the array to set the values of each item into the respective columns of the table using

As a newcomer to the world of javascript, I could really use some assistance with this particular issue. Let's start with a predefined table: <div id="SampleDiv"> <table id="SampleTable"> <tr> <td></td> ...

Is it possible to verify if an event occurred within a specific hour, regardless of the date using Moment.js?

I am in the process of monitoring the usage patterns of a device and displaying users their usage trends by the hour. Essentially, I need to analyze times documents to determine if the device was active between specific hour intervals, such as 6pm to 7pm, ...

How can I retrieve the first key name from an object in AngularJS?

Can you assist me in retrieving the name "GetToolInfo" from the following object? {"GetToolInfo":{ "Item":[ { "_Key":"DefaultPath", "_Value":"C:\\Users\\vkedarix\ ...

Is it possible to incorporate a VueJS .vue component into an HTML page without the need for a WebPack build process?

I'm currently working on a page for an older server-rendered project. For one specific page, I want to incorporate some "dynamic" elements. I decided to import the vuejs CDN script and create an inline Vue object: var v = new Vue(... However, there ...

Issue: Map container not located when implementing a leaflet map with Angular

Here is the complete error message: core.js:6479 ERROR Error: Map container not found. at NewClass._initContainer (leaflet-src.js:4066) at NewClass.initialize (leaflet-src.js:3099) at new NewClass (leaflet-src.js:296) at Object.createMap [a ...

The concept of "Mapping" another property and returning undefined using Angular's HttpClient in Setters and Getters

There seems to be an issue with the behavior of setters and getters that are setting or returning another public property on the object in TypeScript when retrieved using Angular's (v5) HttpClient. Here is the code snippet: export interface NamedEnt ...

Show the <div> element containing an embedded <Link> within the surrounding text

Currently, I am using react-string-replace to conditionally replace text content with links that include #Hashtags and @mentions. Here is an example of the code snippet I am utilizing, directly from the documentation of react-string-replace: content = rea ...

Creating a custom directive in AngularJS to replace the inner text

In my current project, an online exam application, I am using a combination of PHP+MySQL+AngularJs. One crucial section of the project is the "Add Question" feature. Let's consider an example: $simple_question = "A train running at the speed of 60 km ...

Obtaining the identifier of dynamically inserted text components

One of the features on my ASP page allows users to load items into textboxes. Since the number of elements will vary and is not known at the outset, I utilize Javascript to generate new textboxes as needed. <script> var sessionId = 0; function cre ...

Exploring the functionality of Vue.js through Jasmine without the use of JavaScript modules

My task involves investigating unit testing for JavaScript on an older application that does not utilize JS modules (import/export). The app contains JS object/prototypes in external .js files that are imported via script src, and more recently, some Vue 2 ...

Utilizing the onEnter property in the react-router v4 library

In my experience with using react-router v4, I have encountered a bit of confusion. When I click on the <Link> component, the route changes immediately. However, I would prefer to stay on the current route until I fetch the necessary data, rather tha ...

Locating the right selector for adding a div to its parent element in jQuery

I have come across an interesting HTML structure: <div class="dropdownedit"> <div class="dropbtn">textxyz</div> <div class="dropdown-content" style="display: none;"> <div href="#" class="ocond" id="text1">text1</div> &l ...

Display the element containing the targeted content using jQuery

Is there a way to style all li elements with a specific content without assigning them a unique id or class? <ul class="list"> <li>test</li> <li>test2</li> <li>test</li> </ul> I want to make all ...

Glitch causing incorrect images to appear while scrolling through FlashList

Currently, I am using the FlashList to showcase a list of items (avatars and titles). However, as I scroll through the list, incorrect images for the items are being displayed in a mixed-up manner. Based on the explanation provided in the documentation, t ...

What is the best way to manage the input mask?

I am working with an API that provides input masks based on country codes. My goal now is to create a function that will dynamically format the input as the user types. For instance, if the user selects country code +55 and I receive the mask (##)####-### ...

The communication between the child and parent components is failing to function correctly

I'm trying to send data from a child component to a parent component, but the function isn't being invoked correctly. It doesn't seem to work as expected. <router-outlet (activeElement)='getActive($event)'></router-outlet ...

What is the best way to cycle through a nested JS object?

I am currently utilizing useState and axios to make an API call, retrieve the response, and pass it to a Component for rendering. const [state,setState] = useState([]); const getCurrData = () => { axios.get('working api endpoint url').then(r ...