0

Looking at the answer here: https://stackoverflow.com/a/19272093/2547709

Using the $inject syntax my controller ends up looking like this:

class MyCtrl {

  public static $inject: string[] = ['$scope'];
  constructor($scope){
    // stuff
  }
}
// register the controller
app.controller("MyCtrl", MyCtrl);

My question is- what happens if I want to pass my own custom arguments to the constructor as well as any injected variables?:

class MyCtrl {

  public static $inject: string[] = ['$scope'];
  constructor($scope, customArg){
    // stuff
  }
}
// Now how do I pass customArg in without it complaining?
app.controller("MyCtrl", MyCtrl(customArg)); // Nope

I feel like I'm missing something fundamental, using this syntax, does everything you pass in to the .controller() function have to be registered with angular and so I shouldn't be trying to pass in custom arguments at all? Or can I pass in an arbitrary value/object? And if so how?

1
  • 1
    What's an example of something you want to pass? Can you register it with angular using angular.value? Commented Sep 12, 2014 at 16:50

2 Answers 2

3

customArg

You cannot pass in custom argument if angular is going to call the constructor. You can however register other things with Angular e.g. Services,Factories,Values(constants) that angular will pass to the controller for you.

More : https://www.youtube.com/watch?v=Yis8m3BdnEM&hd=1

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

3 Comments

Thanks @basarat, I'm a fan of your videos they're really helping me get to grips with how to use typescript idiomatically with something like angular!
If you're interested, I have a new question up: here
@basarat: A good example is a provider where you abstract the service into a different class. The question is now, do I want to do it? How would you handle this?
0

Sorry for the answer I don't have enough points to comment.

I have the exact same scenario and here is my situation:

export abstract class DataService<T> {

    static $inject = ['$resource'];
    private svc: ng.resource.IResourceClass<ng.resource.IResource<T>>;

    constructor(
        protected $resource: ng.resource.IResourceService, url: string
    ) {
        this.svc = <ng.resource.IResourceClass<ng.resource.IResource<T>>>this.$resource(url, { id: '@id' });
    }

    public get(id: number): ng.IPromise<T> {
        return this.svc.get({ id: id }).$promise;
    }

}

export class MyDataService
    extends DataService<IItem> {


   // big problem here!!!
   constructor(
    ) {
        super("/api/items/:id");
    }

}

Looks like I would have to repeat the injection on every derived class and also pass in the super... so redundant

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.