Angular2 不加载嵌套组件

标签 angular

这应该是最简单的嵌套组件,但是当我(通过 systemjs)加载它时,我在浏览器中看到的只是“计数器”,以及 <counter></counter>已添加到 DOM 中。没有“Hello Counter”的迹象或 counter 的任何处理组件。

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);

最佳答案

您必须在 directives 中指定模板中使用的所有指令View(或 Component)的属性。参见 this plunker .正确代码:

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);

关于Angular2 不加载嵌套组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33212485/

相关文章:

html - Angular 2更改HTML元素的文本

angular - 使用 UIKit 和 Angular 进行测试

angular - 无法在 'value' 上设置 'HTMLInputElement' 属性

angular - 如何从 RxJS 映射运算符( Angular )抛出错误

angular - 如何将 NgRx 8 操作与 NgRx 7 reducer 一起使用

javascript - Angular 4 setInterval 意外行为

angular - 如何使用装饰器使对象的 JSON.stringify 输出 getter ?

Angular 5 动态地将组件插入现有标记?

javascript - 无法绑定(bind)到 'ngSwitchDefault',因为它不是 'ng-template' 的已知属性

Angular Router 在使用自定义 ngDoBootstrap 和 createCustomElement 时忽略 URL