I'm tinkering with Angular2 and have found I am unable to initially populate a html input with values from the controller when the class Constructor is called. Strangely, the input does populates with the bound default data after the user tries to make a change to the value of the input. How do I initially set the value of a html input when the view first loads so that the input is pre-populated with text ?
This is the HTML template...
<div class="row">
<div class="col-md-3">Username:</div>
<div class="col-md-9"><input [(ngModel)]="CurrentUser.Username" /></div>
</div>
<div class="row">
<div class="col-md-3">Password:</div>
<div class="col-md-9"><input [(ngModel)]="CurrentUser.Password" /></div>
</div>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-9"><button class="btn btn-sm btn-primary" (click)="signin()">Sign in</button></div>
</div>
Here is the Component (Controller)...
import {Component} from 'angular2/core'
import {UserService} from '../services/user-current.service';
@Component({
templateUrl: './app/templates/signin.template.html'
})
export class SigninComponent {
private CurrentUser: UserService;
constructor(private userService:UserService) {
this.CurrentUser = userService
}
}
for the sake of reference, I have included the service that loads the data I want displayed in the html input...
import {Injectable} from "angular2/core";
@Injectable()
export class UserService {
private Username = "TEST";
private Password = "Password";
private UserID = 1;
private LoggedIn = true;
constructor() {}
setUser(userObject) {
this.Username = userObject.Username;
this.Password = userObject.Password;
this.UserID = userObject.UserID;
this.LoggedIn = userObject.LoggedIn;
}
}
also for complete clarity, here is the navigation component which is loading the view (including the SigninComponent)...
import {Component} from 'angular2/core'
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router'
import {UsersListComponent} from './users-list.component'
import {DashboardComponent} from './dashboard.component'
import {SigninComponent} from './signin.component'
import {UserService} from '../services/user-current.service';
@Component({
selector: 'nav-top',
templateUrl: './app/templates/nav-top.template.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/users', name:'UsersList', component: UsersListComponent},
{path: '/signin', name:'Signin', component: SigninComponent},
{path: '/', name:'Dashboard', component: DashboardComponent, useAsDefault: true}
])
export class NavTopComponent{
private CurrentUser;
constructor(private userService:UserService) {
this.CurrentUser = userService
}
}
I believe the issue is related to the use of ngOnInit which never seems to fire as the console.log() within the function does not fire. Here is an updated look at the code I'm now using for the SigninComponent...The ngOnInit does not fire.
import {Component,OnInit} from 'angular2/core'
import {UserService} from '../services/user-current.service';
@Component({
selector: 'SigninComponent',
templateUrl: './app/templates/signin.template.html',
providers: [UserService]
})
export class SigninComponent implements OnInit{
public CurrentUser;
constructor(public userService:UserService) {
console.log("constructor");
}
ngOnInit() {
this.CurrentUser = this.userService;
console.log("ngOnInit");
}
}