1

I'm trying to add an Angular function parameter to a directive written in TypeScript but I must not have the syntax right. Here is an example of my directive:

export class NewUserDirective implements ng.IDirective {
    public restrict: string = "E";
    public replace: boolean = true;
    public templateUrl: string = '/views/_dialogs/newUserDialog.html';
    public controller = Directives.NewUserController;
    public controllerAs: string = 'Ctrl';
    public bindToController: boolean = true;
    public scope: any = {
        showDialog: '=',
        saveInProgress: '=',
        onSuccessCallback: '&',
        onClosingCallback: '&'
    };
}

and here is a snippet of my controller:

export class NewUserController {
    static $inject = ["$scope", "$q", "UserServices"];

    public showDialog: boolean;
    public saveInProgress: boolean;
    public onSuccessCallback: () => void;
    public onClosingCallback: () => void;

....

    public save(): void {

        //Flag as saving
        this.saveInProgress = true;

        //Save the plan
        this.UserServices.createUser(this.newUser).then(x => {

            //When finished, hide the modal
            this.showDialog = false;

            //Let parent know new user is created
            this.onSuccessCallback();
        });
    }

}

Everything is working except for the function parameter in the parent controller. I've tried a few different syntaxes in the directive implementation like this:

    <new-user-directive on-success-callback="loadData()" ...

and this:

    <new-user-directive on-success-callback="loadData" ...
0

3 Answers 3

3

Are you using controllerAs for the outer controller that has the loadData method on it? If so, then you would need to create your directive like this (assuming you named our outer controller 'Ctrl'). Notice the Ctrl. in front of loadData()

<new-user-directive on-success-callback="Ctrl.loadData()" ...
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is correct. However, my answer is talking about the controller on the outside of the directive, which we don't know the name of. I said "assuming you named the outer controller 'Ctrl'".
Parent controller is named "c" and a total oversight on my part. Thanks for pointing it out and it is a quick fix. Directive syntax was working correctly. <div ng-controller="UsersController as c">
1

You have controllerAs = 'Ctrl' so on-success-callback="Ctrl.loadData()"

Comments

0

The methods are passed through the $scope and not on the controller. You should create a constructor:

constructor(private $scope, ....){
}

And call the method from the $scope like so:

this.$scope.onSuccessCallback();

2 Comments

The directive definition object set bindToController: true, so the methods would be attached to the controller.
You are correct. I missed that one. As stated here: exploring-angular-1.3-bindToController you do need to use the controller name in the html like you said. upvoting your answer

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.