1

I have following (simplified) directive, and I want to write it with Typescript (still not Angular 2.0).

 angular
        .module('app.components')
        .directive('list', List);

    function List() {
        return {
            restrict: 'E',
            templateUrl: 'modules/components/List/List.html',
            controller: ListController,
            controllerAs: 'vm',
            require: ['ngModel', '?^form'],
            bindToController: true,
            link: LinkFunction,
            scope: {
                'id': '@',
                'name': '@'
            }
        };

        function LinkFunction(scope, element, attrs, controllers) {
            var ngModelCtrl = controllers[0];
            var formCtrl = controllers[1];

            ngModelCtrl.$isEmpty = function(value) {
                return !value || value.length === 0;
            };

            ngModelCtrl.$render = function() {
                scope.vm.form = formCtrl;
                scope.vm.model = ngModelCtrl;
                scope.vm.selected = ngModelCtrl.$modelValue;
            };

            // ... more controller functions
    }
})();

Now, how can I inject the controllers into my Typescript Code:

const template = require('./List.html');

export default class ListComponent{
    static selector = 'list';
    static definition: ng.IComponentOptions = {
        template: template,
        controller: ListComponent,
        bindings: {
            'id': '@',
            'name': '@'
        }
    };

    id;
    name;

    constructor(private controllers) {  // <-- not working
        'ngInject';
    }

    $onInit() {
        let ngModelCtrl = this.controllers[0];
        let formCtrl = this.controllers[1];

        ngModelCtrl.$isEmpty = function(value) {
            return !value || value.length === 0;
        };

        ngModelCtrl.$render = function() {
            this.model = ngModelCtrl;
            this.form = formCtrl;
            this.selected = ngModelCtrl.$modelValue;
        };
    }
}

Hope someone knows the answer or can give me a hint how to achieve my goal, because I searched the web for hours, but didn't find any solution for this.

2
  • thx for your reply, but I know how to write angularjs with typescript (Todd Motto Styleguide), but I dont know, how to inject ngModelController into my component (like in the old angularjs code) Commented Jun 6, 2016 at 6:19
  • Did you manage to inject required controllers? Commented Jul 5, 2016 at 19:06

1 Answer 1

1

Use "require" like explained here https://docs.angularjs.org/guide/component See Intercomponent Communication

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

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.