admin管理员组文章数量:1428675
I am working on a project that involves providing information on vehicle ponents. Each ponent has a set of descriptive fields. I have an "Add another ponent" button that needs to create an identical div container with the same fields. The cloned parent div will need a different ID of course. For example:
In the ponent html file:
<div id="ponent-outer">
<div class="ponent-inner">
Component Content....
</div>
</div>
<button type="button" (click)="clone()">Add Another Component</div>
In the ponent ts file:
export class AppComponent {
clone(){
Need to clone #ponent-outer div and append below the last instance
of that div, then append the div name on the cloned element.
}
}
I am accustomed to using javascript/jQuery to clone elements, but having trouble finding the best approach to EXACTLY what I am trying to acplish in Angular. Is cloning the right approach?
I am working on a project that involves providing information on vehicle ponents. Each ponent has a set of descriptive fields. I have an "Add another ponent" button that needs to create an identical div container with the same fields. The cloned parent div will need a different ID of course. For example:
In the ponent html file:
<div id="ponent-outer">
<div class="ponent-inner">
Component Content....
</div>
</div>
<button type="button" (click)="clone()">Add Another Component</div>
In the ponent ts file:
export class AppComponent {
clone(){
Need to clone #ponent-outer div and append below the last instance
of that div, then append the div name on the cloned element.
}
}
I am accustomed to using javascript/jQuery to clone elements, but having trouble finding the best approach to EXACTLY what I am trying to acplish in Angular. Is cloning the right approach?
Share Improve this question asked Jun 5, 2018 at 18:46 MikronmanMikronman 512 silver badges4 bronze badges 1- 1 You should clone an item in your model, and have your HTML bind to a list. You should not manipulate the DOM directly in Angular apps. – SLaks Commented Jun 5, 2018 at 18:48
1 Answer
Reset to default 4You can use *ngFor
to create DOM elements for each item in an array.
<div id="ponent-outer">
<div *ngFor="let item of items" class="ponent-inner">
Component Content....
</div>
</div>
<button type="button" (click)="append()">Add Another Component</div>
export class AppComponent {
public items: any[];
public append() {
this.items.push({
// values depend on what an item is
});
}
}
This is covered in the documentation in the first chapter of the first tutorial.
https://angular.io/guide/displaying-data#showing-an-array-property-with-ngfor
本文标签: javascriptAngular 6How do I CloneDuplicate a div in typescriptStack Overflow
版权声明:本文标题:javascript - Angular 6 - How do I CloneDuplicate a div in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745453198a2658982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论