无法在角度5中使用ng-include

San*_*nnu 10 typescript angular

对Angular很新,并使用Angular 5和Visual Code进行小型试点项目.我正在尝试使用ng-include,但模板无法显示.

  src
     add-device
        add-device.component.html
        add-device.component.scss
        add-device.component.spec.ts
        add-device.component.ts
     edit-device
        edit-device.component.html
        edit-device.component.scss
        edit-device.component.spec.ts
        edit-device.component.ts
     templates
        device.html
     app.module.ts
     app.coponent.ts
     app.component.html
     ....
Run Code Online (Sandbox Code Playgroud)

简单的device.html代码

<div>Include is working!</div>
Run Code Online (Sandbox Code Playgroud)

附加device.component.html

<div ng-include="" src="'templates/device.html'">
    <div>More unique elements here!</div>
</div>
Run Code Online (Sandbox Code Playgroud)

我也试过跟随

<div ng-include="" src="'templates/device.html'"></div>

<div ng-include="'templates/device.html'"></div>
Run Code Online (Sandbox Code Playgroud)

以下给出错误,无法识别ng-include.

<ng-include src="'templates/device.html'"></ng-include>
Run Code Online (Sandbox Code Playgroud)

目标是使用来自添加和编辑设备组件的device.html模板代码.我正在使用typescript来管理项目.当我在调试器中查看Sources时,我甚至没有在源列表中看到device.html文件.

Mon*_*Kin 19

Angular没有ng-include像AngularJS那样的.

在Angular中,您可以创建这样的组件:

@Component({
    selector: 'app-device',
    templateUrl: './device.html'
})
class DeviceComponent {}
Run Code Online (Sandbox Code Playgroud)

然后,而不是:

<div ng-include="'templates/device.html'">
Run Code Online (Sandbox Code Playgroud)

你做:

<app-device></app-device>
Run Code Online (Sandbox Code Playgroud)

  • 如何动态使用选择器,例如在ng-include中我们可以指定文件名. (10认同)