我为我的组件 [grid] 创建了自定义管道。在管道声明中,我可以获取我的组件实例,但同时我想获取我的数据源值是否在管道声明中发生变化。有可能得到吗?
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { GridAllModule } from '@syncfusion/ej2-angular-grids';
import { NumericTextBoxAllModule } from '@syncfusion/ej2-angular-inputs';
import { ToolbarModule } from '@syncfusion/ej2-angular-navigations';
import { AppComponent, DefaultPipe, DefaultPipe1 } from './app.component';
@NgModule({
declarations: [
AppComponent,
DefaultPipe,
DefaultPipe1
],
imports: [
BrowserModule, GridAllModule, ToolbarModule, NumericTextBoxAllModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
app.component.ts
import {
NgModule, Component, Pipe, PipeTransform, OnInit, Input, IterableDiffers, DoCheck,
ViewChild, ChangeDetectorRef, SimpleChanges, WrappedValue, SimpleChange, OnChanges
} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { orderDetails } from './data';
import { orderDetails1 } from './datas';
import { GridComponent } from '@syncfusion/ej2-angular-grids';
// import { SidebarService } from './service';
@Pipe({ name: 'defaultImage' })
export class DefaultPipe implements PipeTransform {
transform(value: string, fallback: string, ): string {
let image = '';
if (value) {
image = value;
} else {
image = fallback;
}
return image;
}
}
@Pipe({
name: 'defaultImage1',
pure: false
})
export class DefaultPipe1 implements PipeTransform {
constructor(private _ref: ChangeDetectorRef) { }
transform(value: string, fallback: string, ): string {
// let Data1 = '';
// if (value.length !== 0) {
// Data1 = value;
// } else {
// Data1 = fallback;
// }
// return Data1;
}
}
@Component({
selector: 'app-my',
template: `
<img [src]="imageUrl | defaultImage:'http://s3.amazonaws.com/uifaces/faces/twitter/sillyleo/128.jpg':true"/>
<div class="control-section">
<ejs-grid #gridref id='check' [dataSource]='data | defaultImage1:gridref' height='350'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=90></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=120></e-column>
<e-column field='Freight' headerText='Freight' textAlign='Right' format='C2' width=90></e-column>
<e-column field='OrderDate' headerText='Order Date' textAlign='Right' format='yMd' width=120></e-column>
</e-columns>
</ejs-grid>
</div>
`
})
export class AppComponent implements OnInit, DoCheck {
imageUrl: String = '';
public data: Object[] = [];
@ViewChild('gridref')
public refGrid: GridComponent;
ngOnInit(): void {
this.data = orderDetails;
}
}
在这里,我已经初始化了我的管道,如下所示:
<ejs-grid #gridref id='check' [dataSource]='data | defaultImage1:gridref' height='350'>
我试过这样 [ http://blog.bogdancarpean.com/how-to-watch-for-changes-an-arrayobject-on-angular-2/ ] 但我无法达到我的要求
@Input() data: Object[] = [];
differ: any;
constructor(differs: IterableDiffers) {
this.differ = differs.find([]).create(null);
}
ngDoCheck() {
const change = this.differ.diff(this.data);
console.log(change);
}
在这种情况下,我怎样才能达到我的要求。请提供任何想法。