3

I want to create a variable inside a constructor (or function). I don't wanna created in the entire class. Is that possible?

This is my code

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { TranslatorService } from '../../../../core/translator/translator.service';
import { CarBrandService } from '../car-brand.service';
import { Response } from '../../../../shared/models/response';
import { SessionService } from '../../../../shared/services/session/session.service';
import { ServerService } from '../../../../shared/services/server/server.service';
import { ToasterService, ToasterConfig } from 'angular2-toaster/angular2-toaster';
import { Router } from '@angular/router';
import { CarBrand } from '../car-brand';
import { SharedModule } from '../../../../shared/shared.module';
import { Table } from '../../../../shared/models/table';
import { TableRow } from '../../../../shared/models/table-row';
import { TableAction } from '../../../../shared/models/table-action';
import { ListTableComponent } from '../../../../shared/components/list-table/list-table.component';

@Component({
  selector: 'car-brand-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss'],
  providers: [CarBrandService, Response, SessionService, ServerService,             ListTableComponent, TableAction, Table, TableRow]
})
export class CarBrandIndexComponent implements OnInit {
  error : String;
  total : number;
  items : Array<CarBrand>;
  table : Table;
  constructor(public translator: TranslatorService, private mainService : CarBrandService, public sessionService : SessionService, public response : Response, private toasterService: ToasterService, private router: Router) {

    var action : TableAction;
    action.value = "/view/";
    action.type = "link";
    this.table.actions.push(action);
    var action : TableAction;
    action.value = "/edit/";
    action.type = "link";
    this.table.actions.push(action);
    this.table.columns = ["MODULES.COMMON.LOGO", "MODULES.COMMON.NAME", "MODULES.COMMON.ACTIONS"];    
    this.getItems(1,10);
  }

  getItems(pageNumber : number, pageSize : number){
//Do something
            });        
        }
        else
          this.error = result.code;
    });
  }
  ngOnInit() {
       }
}

This is TableAction

export class TableAction {
    type : String;
    value : String;
    name : String;
    icon : String;
}

But in the following line var action : TableAction; It tells me that action is undefined. Is there a way to fix this without havong to declare the var below the class?

1
  • its undefined because you never initialize the value I think. So try let action: TableAction = new TableAction(); Commented Feb 13, 2017 at 11:28

2 Answers 2

2

var action : TableAction; You just tell about 'action' type. Now if u trying assign to action something like {a: 1, b: 2} -- you will see an error.

but if u write var action : TableAction = new TableAction() -- you will have an instance in your scope (contructor/function) as you wanted and also define a type for the variable 'action'.

Advice: use let instead of var

let action = new TableAction(); 

A behavior of variables which were defined with the keyword 'let' are more predictable.

constructor() {
  let action1 = new TableAction("/view/", "link");
  this.table.actions.push(action1);

  let action2 = new TableAction("/edit/", "link");
  this.table.actions.push(action2);
  // ...
}

export class TableAction {
  type : String;
  value : String;
  name : String;
  icon: String;

  constructor(value, type) { 
    this.value = value;
    this.type = type;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
var action : TableAction

is a declaration.

var action : TableAction=new TableAction();

or

var action : TableAction = {};

is a definition. You are trying to set object properties of action which is not defined

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.