0

In my angular 2 application i tried to bind view data using ngmodel,but it didnt works as i expected.

event.component.html

 <div  class="form-group">
     <label for="comment">About Us:</label>
         <input type="text" name="aboutus" class="form-control" 
 [(ngModel)]="home.aboutus" required placeholder="aboutus"/>{{home.aboutus}}

 </div>

homemenu.ts

  export class Home {
  aboutus: string;
    }

eventcomponent.ts export class EventComponent { home:Home; } constructor() { }

3
  • because home is undefined Commented Jun 16, 2017 at 6:07
  • constructor(){} must be inside class EventComponent Commented Jun 16, 2017 at 6:14
  • @Ajith V Manali what error? Commented Jun 16, 2017 at 6:30

5 Answers 5

1

its always good idea to initialise class variables in constructor like this

export class EventComponent {
   home:Home = null; // or home:Home; 
   constructor() { 
        this.home = new Home()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

It should be just aboutus

  <input type="text" name="aboutus" class="form-control" 
 [(ngModel)]="aboutus" required placeholder="aboutus"/>{{aboutus}}

Comments

1

You need to initialize home in event component.ts:

export class EventComponent {
       home:Home = new Home(); 
       constructor() { }
   }

Comments

1

Since home is not initialized that's why home.aboutus is not bind with html template.

Try Below Code

export class EventComponent {
    home:Home;

    constructor() {
        this.home = new Home();
    }

}

Comments

0

bind the home instance property instead and bind [(ngModel)]="home.aboutus" spit the entered value using string interpolation {{home.aboutus}}.

export class EventComponent{
      home: Home;
      constructor() {
        this.home = new Home();
      }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.