简体   繁体   中英

My Controller constructor is never called (AngularJS)

I am setting up a small angularjs app inside a Sharepoint web part. This is the first time doing this, but I made a demo work in this setting before doing the proper code. My problem now is that I think everything is properly hooked up, but the Controller constructor is never called? I am aware that the code is not complete, but at the very least I should enter the constructor and call the service. The only alerts I see are the ones in the App file. Here are the different files:

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']);
});

I found the problem buried in this post: 'unknown provider error' in Angular app

The sentence: "Services don't have $scope" was the key. Once I removed the $scope as an input parameter to the service constructor, everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM