Skip to content

Instantly share code, notes, and snippets.

@s-o-r-o-u-s-h
Last active September 20, 2016 14:53
Show Gist options
  • Select an option

  • Save s-o-r-o-u-s-h/3138059bb71080b8e9de to your computer and use it in GitHub Desktop.

Select an option

Save s-o-r-o-u-s-h/3138059bb71080b8e9de to your computer and use it in GitHub Desktop.
//datatable.pipe.ts
import {Pipe} from "angular2/core";
@Pipe({name: "iterateObject", pure: false})
export class IterateObjectPipe {
transform(value:any, args:string[] = null):any {
return Object.keys(value).map(key => value[key]);
}
}
//app.component.ts
import {Component,OnInit} from "angular2/core";
import {Datatable} from "../interfaces/datatable";
import {DatatableService} from "./datatable.service";
import {IterateObjectPipe} from "./datatable.pipe";
@Component({
selector: 'app',
templateUrl: 'app/main/app.component.html',
providers: [DatatableService],
pipes: [IterateObjectPipe]
})
export class AppComponent implements OnInit{
public init:Datatable;
constructor(public _DatatableService: DatatableService){}
ngOnInit():any {
this._DatatableService.getDatatables().subscribe(res=>{this.init = res});
}
}
//datatable.service.ts
import {Injectable} from "angular2/core";
import {Http} from "angular2/http";
import "rxjs/add/operator/map";
@Injectable()
export class DatatableService {
getDatatables() {
return this._http.get('init.json').map(res => res.json());
}
constructor(private _http: Http){}
}
//app.component.html
<div class="col-xs-12 col-sm-6 col-md-3 modules-item" *ngFor="#module of init|iterateObject">
<h4 class="text-center">test</h4>
</div>
@s-o-r-o-u-s-h
Copy link
Author

TypeError: Cannot convert undefined or null to object in [init|iterateObject in AppComponent@6:62]

@peterps-health
Copy link

too much magic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment