Scope-Variable für Direktive ist im Konstruktor nicht definiert, wenn bindToController @ verwendet wi

So habe ich eine Direktive, die beim Erstellen einen Wert annehmen sollte. Nennen wir die DirektiveMyDirective. Um es zu verwenden und ihm einen Wert zu übergeben, können Sie folgendermaßen vorgehen:

<my-directive value="'I will be undefined'"></my-directive>

Ich verwende TypeScript, daher möchte ich Klassen ohne @ habe$scope, also dafür binde ich an controller.

class MyDirectiveController {
    public value:string;

    constructor(private $scope: ng.IScope) {
        // I wanna do something with this.value at this point
        // But it is undefined ...
        console.log(this.value);

        $scope.$watch('value', this.valueDidChangeCallback).bind(this);

    }

    valueDidChangeCallback:any = () => {
        // Now I can do the thing I wanted to do ...
        console.log(this.value);
    };
}

export class MyDirectiveDirective {
    restrict: string = 'E';
    templateUrl: string = 'my-directive.html';
    bindToController: boolean = true;
    controllerAs: string = 'vm';
    scope:any = {
        'value': '='
    };

    controller: any = ($scope: ng.IScope) => new MyDirectiveController($scope);

    constructor() {}

    static Factory(): ng.IDirective {
        return new LicenseOverviewPageDirective();
    }
}

So ist das Problem, dass ich @ verwenden mu$watch weil der an die Direktive übergebene Wert ("Ich werde undefiniert") noch nicht gesetzt ist, wenn ich mich im Konstruktor befinde (wo ich ihn brauche ...)

Gibt es einen besseren Weg, dies ohne Uhr zu tun?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage