1

My goal was to use dependency injection of a constant value (the base URL) to use in defining the component's templateUrl property. Nothing so far has worked. The code below is in Typescript

Some explanation: app.core.Iconstants holds the baseUrl value. app.core.AngularGlobals.appCoreConstants is the string representing "app.core.Constants".

Mind you, I'm just cutting my teeth of Angular.

namespace donationEntry.Components{ "use strict";

class test1 implements ng.IComponentOptions {
    public static id: string = AngularGlobals.donationEntry + ".test1";
    bindings: any;
    controller: any;
    templateUrl: string;

    constructor(clientContext: app.core.IConstants) {
        this.bindings = {
            textBinding: '@',
            dataBinding: '<',
            functionBinding: '&'
        };
        this.controller = TestController;
        this.templateUrl = clientContext.baseUrl + "Areas/ng/app/fundraising/donationEntry/components/test1/test1.html";
    }
}

// register the controller with app
angular.module(AngularGlobals.donationEntry)
    .component("test1", [app.core.AngularGlobals.appCoreConstants, (c) => new test1(c)]);

}

When I run this, I end up with the following error:

angular.js:61 Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:modulerr] Failed to instantiate module donationEntry due to: TypeError: t.charAt is not a function

2
  • Says you are calling charAt on an object that doesn't have charAt, but I don't see charAt anywhere in the snippet you provided, are you sure this is where the problem is? Commented May 18, 2016 at 14:32
  • Having circumvented the problem, I never dug deeper, however it seems related to the shortcut injection syntax I used. Commented May 18, 2016 at 18:11

2 Answers 2

0

This appears to work, though I wouldn't call it the most eloquent of answers. Again, written in Typescript:

namespace donationEntry.Components{ "use strict";

class test1 implements ng.IComponentOptions {
    public static id: string = AngularGlobals.donationEntry + ".test1";
    bindings: any;
    controller: any;
    templateUrl: string;


    constructor(clientContext: app.core.IConstants) {
        this.bindings = {
            textBinding: '@',
            dataBinding: '<',
            functionBinding: '&'
        };
        this.controller = TestController;
        this.templateUrl = clientContext.baseUrl + "Areas/ng/app/fundraising/donationEntry/components/test1/test1.html";
    }
}

// register the controller with app
var clientContext = <app.core.IConstants>angular.injector(['ng', app.core.AngularGlobals.appCore]).get(app.core.AngularGlobals.appCoreConstants);
angular.module(AngularGlobals.donationEntry)
    .component("test1", new test1(clientContext));

}

Sign up to request clarification or add additional context in comments.

Comments

0

I try to stay away from Angular's injection or module mechanism whenever I can, preferring straight ES6.

You can do:

//clientContext.ts
export = { baseUrl: 'whateva' }

And then import like this:

import clientContext from './path/to/clientContext'

1 Comment

Thanks. That would be great, however even the client context path itself would be subject to needing a dynamic base literal (our clients would have a different one than us). Certainly understand your dislike for injection, heh.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.