简体   繁体   English

在Angular2组件类中将函数转换为变量

[英]Turn Function into Variable in Angular2 Component Class

How do I change the cellRenderer function into a reusable variable? 如何将cellRenderer函数更改为可重用变量? I have tried using a constructor, and declaring a variable, seems simple, but any help would be appreciated. 我试过使用构造函数,并声明一个变量,这似乎很简单,但是任何帮助将不胜感激。

import {Component} from 'angular2/core';

@Component({
    selector: 'app',
    template: '<ag-grid-ng2 class="ag-fresh" style="height: 300px"    [columnDefs]="columnDefs"   [rowData] = "rowData"></ag-grid-ng2>',
    directives: [(<any>window).ag.grid.AgGridNg2]
})
export class SampleAppComponent {

    columnDefs = [
        { headerName: "Make", field: "make" },
        { headerName: "Model", field: "model" },
        {
            headerName: "Price",
            field: "price",
            cellClass: 'rightJustify',
            cellRenderer: function (params: any) {
                return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
            }
        }
    ];
}

What am I doing wrong here? 我在这里做错了什么?

export class SampleAppComponent {

constructor() {
    this.convertToMoney = function (params: any) {
        return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
    };


columnDefs = [
    { headerName: "Line Type", field: "line" },
    { headerName: "Expense Type", field: "expense" },
    {
        headerName: "Totals",
        field: "totals",
        cellClass: 'rightJustify',
        cellRenderer: convertToMoney()
    }
];

Not entirely sure what you are trying to achieve here, and not to sound rude, but perhaps checking the proper syntax of typescript first will help you achieve, and understand what you must do. 不能完全确定您要在此处实现的目标,并且听起来并不粗鲁,但是也许先检查打字稿的正确语法将有助于您实现并了解您必须做的事情。

Anyways.. I will try to help you a bit, I believe you would like a class like this? 无论如何..我会尽力帮助您,我相信您想要这样的课程吗?

import {Component} from 'angular2/core';

@Component({
    selector: 'app',
    template: '<ag-grid-ng2 class="ag-fresh" style="height: 300px"    [columnDefs]="columnDefs"   [rowData] = "rowData"></ag-grid-ng2>',
    directives: [(<any>window).ag.grid.AgGridNg2]
})
export class SampleAppComponent {

    public columnDefs : any[] = [
        { headerName: "Line Type", field: "line" },
        { headerName: "Expense Type", field: "expense" },
        {
            headerName: "Totals",
            field: "totals",
            cellClass: 'rightJustify',
            cellRenderer: this.convertToMoney //reference to class' convertToMoney method
        }
    ];       

    constructor() {}

    private convertToMoney(params: any) : string {
        return '$' + params.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); //thanks http://stackoverflow.com/users/28324/elias-zamaria
    } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM