4

I have been trying to follow this tutorial to create a nested tree view. The tutorial is in typescript while I am trying to do a similar thing in javascript with Angular2.

In the typescript code, the tree-view component looks like so:

import {Component, Input} from 'angular2/core'; 
import {Directory} from './directory'; 
@Component({
    selector: 'tree-view',
    templateUrl: './components/tree-view/tree-view.html',
    directives: [TreeView] 
}) 
export class TreeView {
    @Input() directories: Array<Directory>; 
}

In javascript that should convert to:

TreeView = ng.core
  .Component({
    selector: 'tree-view',
    templateUrl: './components/tree-view/tree-view.html',
    directives: [TreeView],
    inputs: ['directory'],
  })
  .Class({
    constructor: function() {
    },
  });

However, javascript throws the following error:

EXCEPTION: Unexpected directive value 'undefined' on the View of component 'function () {'

I believe it's because I'm calling directives: [TreeView] before TreeView has been fully defined. If I remove that directive line, the error goes away. However, I don't know why it would work in typescript and not javascript if typescript simply compiles to javascript. This is the compiled javascript from the typescript code. I'm not sure what I'm missing. Any help would be super appreciated.

2
  • Try with directives : [ng.core.forwardRef(function() { return TreeView; })] Commented Jan 21, 2016 at 1:29
  • Eric, you are an angel among programmers. That worked like a charm. If you make it an answer, I will accept it. Do we know how/why this solution works? Commented Jan 21, 2016 at 18:03

1 Answer 1

3

This question has been answered a few times

First of all classes are not hoisted. Quoting from MDN

An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not. You first need to declare your class and then access it [...]

The documentation for forwardRef says

For instance, forwardRef is used when the token which we need to refer to for the purposes of DI is declared, but not yet defined. It is also used when the token which we use when creating a query is not yet defined.

So it's as easy as adding forwardRef to your code

directives : [ng.core.forwardRef(function() { return TreeView; })]

You can read more about this subject

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

1 Comment

Hi Eric - could you please share a plnkr/fiddle that demonstrates using forward ref in the same array as other directives used by the component? IE directives : [ RouterLink, CORE_DIRECTIVES, ng.core.forwardRef(function() { return TreeView; })]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.