Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Using AngularJS 1.5 and TypeScript I am writing a directive and want to define an attribute in my view that gets passed into the directive and then used to output to a template. I have tried to follow some examples such as this one, but my directive template isn't binding to the attribute.

HTML from page:

<my-directive name="John"></my-directive>

Directive:

module myApp.directives {
    "use strict";

    export class myDirective {
      static directiveId = 'myDirective';
      scope = {};
      bindToController = {
        name: '='
      };
      restrict = 'EA';
      controller = controllers.NameController.ControllerId;
      controllerAs = 'vm';
      template = '<h1>Name: {{ vm.name }}</h1>';

      static $inject = [];
      constructor() { }

      static instance() {
        return new myDirective();
      }
    }
    angular.module("myApp").directive(myDirective.directiveId, myDirective.instance);
}

Controller (not really sure I need anything in the constructor?):

module myApp.controllers {
    "use strict";

    export class NameController {
        static ControllerId = "NameController";
        name: string;

        constructor(name: string){
            this.name = name;
        }
    }

    angular.module("myApp").controller(NameController.ControllerId, NameController);
}
share|improve this question
up vote 1 down vote accepted

Your bindToController should accept a string litteral and not a bound property, i.e.

bindToController = { name: '@' };

the = will try to evaluate a property called John in your syntax, which does not exist.

Also, your constructor does not need any parameter, and for what I see can be totally removed.

share|improve this answer
    
Thanks! That solved my problem. – Scott Ferguson Mar 3 at 17:21
    
You're welcome! – Hugues Stefanski Mar 3 at 17:54

I'm not sure this helps, neither how angular is merging instance of NameController and $scope together, but I can imaging that name property of NameController instance is shadowing $scope.name.

Would you try removing:

name: string;

constructor(name: string) {
  this.name = name;
}

?

share|improve this answer
    
When I remove that, my view changes from displaying "Name: {{ vm.name }}"(braces are visible) to "Name: "(no braces but also no data). – Scott Ferguson Mar 2 at 20:15

I believe the attribute you need should be defined on the directive scope, seeing as you are already using isolated scope. Doesn't look like you are using bindToController anywhere.

Instead of this:

scope = {};
bindToController = {
  name: '='
};

Try with this (older syntax):

scope = {
  name: '='
};

Also, the parameterized constructor in controller won't run since you aren't new-ing up the class (in the line below, from your second code snippet)

angular.module("myApp").controller(NameController.ControllerId, NameController);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.