angular-ngFor

假设这样定义模版:

<span *ngFor="let item of items">{{item}}</span>

然后由编译器将其转换为以下内容:

<ng-template let-item [ngForOf]="items">
    <span>{{item}}</span>
</ng-template>

然后,Angular将ngForOf指令应用于模板元素。 由于此指令的宿主元素是template,因此将注入templateRef。 它还将注入充当锚元素的viewContainerRef并将用于在旁边添加DOM元素:

  constructor(
       private _viewContainer: ViewContainerRef, 
       private _template: TemplateRef<NgForOfContext<T>>,
       ......

该指令将ngForOf定义为@Inout,然后等待它被初始化并创建一个differ:

 ngOnChanges(changes: SimpleChanges): void {
      const value = changes['ngForOf'].currentValue;
      this._differ = this._differs.find(value).create(this.ngForTrackBy);
      ......

然后,在每个检查检测周期中,它使用以下差异将值与以前的值进行比较:

  ngDoCheck(): void {
    if (this._differ) {
      const changes = this._differ.diff(this.ngForOf);
      if (changes) this._applyChanges(changes);
    }
  }

如果值更改,它将执行以下操作来应用更改:

  1. generates embedded view context for each item in items

    context = new NgForOfContext(null !, this.ngForOf, -1, -1)

  2. creates embedded view with this context using the templateRef which effectively renders new value in the DOM

    this._viewContainer.createEmbeddedView(
    this._template, context , currentIndex);

  3. adds relevant values to context

    viewRef.context.index = i;
    viewRef.context.count = ilen;
    viewRef.context.$implicit = record.item;`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular中使用angular-gridster库,可以通过使用其提供的事件和方法来获取拖拽后的数据。 首先,我们需要在HTML文件中设置Gridster组件,并声明一个用于展示数据的数组: ```html <gridster [options]="gridsterOptions" (gridsterItemChange)="onItemChange($event)"> <gridster-item *ngFor="let item of gridsterItems" [item]="item"> <!-- content --> </gridster-item> </gridster> ``` 在组件的.ts文件中,需要定义gridsterOptions和gridsterItems变量,并使用相关的事件和方法来获取拖拽后的数据: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { gridsterOptions = { // gridster options }; gridsterItems = [ // initial items ]; onItemChange(event: any) { // get gridster item change event console.log(event); // here you can access the dragged item's updated data } } ``` 在onItemChange方法中,可以通过event参数访问拖拽后的数据。它包含了当前拖拽的GridsterItemComponent实例,我们可以从中获取更新后的数据。 例如,可以通过event.item获取更新后的位置信息、尺寸信息等。如果有其他自定义的数据需要获取,可以在GridsterItemComponent中设置相关属性并在event.item中访问。 以上是使用angular-gridster获取拖拽后数据的基本方法,你可以根据自己的需求进行进一步定制和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值