Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

typescript - Unable to use ng-include in angular 5

Pretty new to Angular and doing a small pilot project using Angular 5 and Visual Code. I am trying to use ng-include and the template doesn't get displayed.

  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
     ....

simple device.html code

<div>Include is working!</div>

add-device.component.html

<div ng-include="" src="'templates/device.html'">
    <div>More unique elements here!</div>
</div>

I also tried following

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

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

Following gives error, ng-include is not recognized.

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

Objective is to use device.html template code from both add and edit device components. I am using typescript to manage the project. When i look at the Sources in debugger, i do not even see device.html file in source list.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Angular doesn't have a ng-include as AngularJS used to have.

In Angular, you can create a component like this:

@Component({
    selector: 'app-device',
    templateUrl: './device.html'
})
class DeviceComponent {}

And then, instead of:

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

you do:

<app-device></app-device>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...