[Angular 2] Move and Delete Angular 2 Components After Creation

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 componentRef
        this.last.instance.message = "I am last"; // using componentRef‘s instance prop to access the component prop
        this.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
时间: 2024-10-12 12:42:16

[Angular 2] Move and Delete Angular 2 Components After Creation的相关文章

angular的跨域(angular百度下拉提示模拟)和angular选项卡

1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).success().error(); $http.jsonp(url,{params:{wd:'',cb:'JSON_CALLBACK'}}).success().error(); 注意jsonp中cb在angular中规定只能使用JSON_CALLBAC $watch(谁,做什么,false): 谁指

[Angular 2] Generate and Render Angular 2 Template Elements in a Component

Angular 2 Components have templates, but you can also create templates inside of your templates using Angular 2 ViewContainer’s createEmbeddedView for more visual control of how you want to generate elements inside of your Angular 2 templates. import

【angular.js】UI-Router之angular路由学习

AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由 通常我们的URL形式为http://jtjds.cn/first/page,但在单页web应用中angularjs通过#+标记实现,比如下面的页面,将是下文中的路由列子. http://192.168.1.109:8000/angular-program/src/main.html#/pagetable/page1 http://192.168.

简话Angular 01 为什么要学Angular

1. JQuery vs Javascipt 问两个问题: 1) 你用过JQuery吗?当然! 2) 感觉JQuery相对于原生纯Javascript优势在哪里? 我的答案:JQuery更直接方便,兼容性好,代码少,好理解,快速开发,Bug少…… 2. Angular vs JQuery 参照JQuery vs Javascript 3. Angular的前途怎么样 假如今天是2008年,有人问JQuery前途怎么样,你知道吗?现在你知道JQuery前途了吗? 4. Angular可以不学吗 不

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

Artical --> BYPASSING PROVIDERS IN ANGULAR 2 Here trying to solve one problem: On the left hand side of tree, there are 4 green blocks and 1 blue block. Meaning that three green dataService will use 'OtherProvider' which in an instance of DataService

在Angular外部使用js调用Angular控制器中提供的函数方法或变量

Html代码如下所示: 1 <!DOCTYPE html> 2 <html ng-app="myApp" id="myApp"> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <title>Test</title> 6 <script src="~/Co

[Angular Directive] 1. Write an Angular Directive

Angular 2 Directives allow you manipulate elements by adding custom behaviors through attributes. This lesson covers how to create a Directive and attach its behavior to an element. import {Directive, HostBinding} from '@angular/core'; @Directive({ s

[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface

When communicating with some backend API, data travels over the network using the HTTP protocol. As such, failures may occur, be it on our own device (i.e. the browser) or on the server-side which may not be available or unable to process our request

[Angular 2] Generate Angular 2 Components Programmatically with entryComponents &amp; ViewContainerRef

You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but you must always remember to add the components you want to generate to your list of entryComponents otherwise the compiler will optimize the componen