Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 not loading nested components

Tags:

angular

This should be the simplest possible nested components, but when I load this (via systemjs) all that I see in the browser is "Counters", and the <counter></counter> has been added to the DOM. There is no sign of "Hello Counter" or of any processing of the counter component.

import angular from 'angular2/angular2';

var AppComponent = angular
  .Component({
    selector: 'my-app',
    template: '<h1>Counters</h1><counter></counter>'
  })
  .Class({
    constructor: function () { }
  });

angular
  .Component({
    selector: 'counter',
    template: '<h2>Hello counter</h2>'
  })
  .Class({
    constructor: function () {}
  });

angular.bootstrap(AppComponent);
like image 957
Simon H Avatar asked Dec 14 '22 11:12

Simon H


1 Answers

You must specify all directives which are used in your template in directives property of View (or Component). See this plunker. Correct code:

var Counter = angular
  .Component({
    selector: 'counter',
    template: '<h2>Hello counter</h2>'
  })
  .Class({
    constructor: function () {}
  });

var AppComponent = angular
  .Component({
    selector: 'my-app',
    directives: [Counter], // <-- Here we are!
    template: '<h1>Counters</h1><counter></counter>'
  })
  .Class({
    constructor: function () { }
  });

angular.bootstrap(AppComponent);
like image 187
alexpods Avatar answered Jan 21 '23 22:01

alexpods