0
export class DashboardComponent implements OnInit {     
@ViewChild('modal') modal: ModalComponent;
        dashboards: IDashboard[];
        dashboard: IDashboard;    
    constructor(private fb: FormBuilder, private _dshboardService: DashboardService, private router: Router) {
                var sesssion = sessionStorage.getItem('EmpDetail');
                var ses = JSON.parse(sesssion);
                var EmpNo = ses[0].EmpNumber;
                var CompanyCode = ses[0].CompanyCode;
                this.dashboard = {};
                this.dashboard.EmpName = ses[0].EmpName;
                this.dashboard.EmpNumber = ses[0].EmpNumber;
            }
}

this shows the below error on the line this.dashboard = {};

Error TS2322 Type '{}' is not assignable to type 'IDashboard'.
Property 'EmpName' is missing in type '{}'.

I need to assign the value to the properties of dashboard model and bind the same in html component

Below is my IDashboard:

export interface IDashboard {
    EmpName: string,
    EmpNumber: string,
    EmailId: string,
    FirstName: string,
    LastName: string,
    Gender: string,
    Program:string
}
2
  • can you show the IDashboard class? Commented Dec 5, 2017 at 6:41
  • @SurajRao I had updated my Question with IDashboard class. Check it now Commented Dec 5, 2017 at 7:21

2 Answers 2

2

Error there:

this.dashboard = {};

this.dashboard has type IDashboard, i guess it has at least one to optional field. So you can't set object without setting this required fields. Something like:

interface Person {
  name: string;
}

const person = {} // Error
const person1 = {name: 'Test'} // No Error


interface Person1 {
 name?: string; //name is optional field
}
 const person = {} // No Error
Sign up to request clarification or add additional context in comments.

Comments

1

you need to declare and initialize it with empty object inside the constructor or ngOninit.

You need to have the Dashboard declared in the component as,

const dashboard: Dashboard= {  param1: "", param2: ""...etc};

3 Comments

above constructor !
im getting errors if I add this code above constructor thats why i asked.Following are my error Build:A class member cannot have the 'const' keyword. Build:Duplicate identifier 'dashboard'. Build:Cannot find name 'Dashboard'.
it's just a sample code have a look here stackoverflow.com/questions/47618181/…

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.