[Angular 2] Move and Delete Angular 2 Components After Creation
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[Angular 2] Move and Delete Angular 2 Components After Creation,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1978字,纯文字阅读大概需要3分钟。
内容图文
![[Angular 2] Move and Delete Angular 2 Components After Creation](/upload/InfoBanner/zyjiaocheng/1310/85b57d7554954494bf2ebbb3e1265b33.jpg)
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to reorder the components. This is especially helpful when you want an event to trigger layout changes from your generated components.
import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver } from ‘ @angular/core ‘ ; import {SimpleService} from " ../../serivces/simple.service " ; import {WidgetThree} from " ../widgets/widget-three.component " ; @Component({ moduleId: module.id, selector: ‘ home ‘ , templateUrl: ‘ home.component.html ‘ }) export class HomeComponent { last; @ViewChild( ‘ container ‘ , { read: ViewContainerRef }) container; constructor( private resolver: ComponentFactoryResolver, private simpleService: SimpleService) { } ngAfterContentInit(){ const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree); this.container.createComponent(WidgetFactory); this.container.createComponent(WidgetFactory); this.container.createComponent(WidgetFactory); this.container.createComponent(WidgetFactory); this.last = this.container.createComponent(WidgetFactory); // return a componentRefthis.last.instance.message = "I am last"; // using componentRef‘s instance prop to access the component propthis.last.instance.renderer.setElementStyle( this.last.instance.input.nativeElement, ‘color‘, ‘red‘ ); } onClick(){ const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree); const comRef = this.container.createComponent(WidgetFactory, 2); comRef.instance.message = "I am third"; } moveup(){ const randomInx = Math.floor(Math.random() * this.container.length); this.container.move(this.last.hostView, randomInx); } }
In the code, will just move the last WidgetTree component to some random position.
How to get the host elemnt?:
this.last.hostVie
How to get the children number inside the host veiw?
this.container.length
原文:http://www.cnblogs.com/Answer1215/p/5901424.html
内容总结
以上是互联网集市为您收集整理的[Angular 2] Move and Delete Angular 2 Components After Creation全部内容,希望文章能够帮你解决[Angular 2] Move and Delete Angular 2 Components After Creation所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。