0

I have the code for drop down it can't be display when page is loaded. The drop down is display once clicked refresh button.

html:

<div class="form-group row">
    <select id="select111" name="role"  placeholder="Roles" class="form-control selectpicker" style="width:476px;">
       <option *ngFor="let role of roles" [value]="role">{{role}}</option>
     </select>        
</div>

component.ts:

export class RegistrationComponent implements OnInit {
submitted = false;
loading= false;
// registrationForm: FormGroup;

 roles = ['User', 'Admin' ,'Supervisor'];

  registers={
   username:'',
         email:'',
         password:'',
         companyName:'',
         role:''
  }

    registervalid=false;

    constructor(private authService: AuthService,public router:Router,public http:Http) {
    }

    ngOnInit(): void {

         this.registers.role=this.roles[0];
    }

    onDefault($event){
    $event.preventDefault();
    console.log('selected: ' + $event.target.value);

}    
   CreateAccount(){
     this.submitted = true;

   let headers = new Headers({ 'Content-Type': 'application/json' });
   let options = new RequestOptions({ headers: headers });
   let body = JSON.stringify(this.registers);
   console.log('the registers is a' + body)
   this.http.post('http://localhost:3000/api/user', body,options).map((res:Response) => res.json())
   .subscribe( data =>{ 
   this.registers = data; 
   console.log(' data object' +JSON.stringify(data));
   if (data) {
   console.log('insert sucess msg' +JSON.stringify(this.registers));
        alert('login is successfully');
         this.router.navigate(['/login ']);
           }
},
    err => console.error(err),
    () => console.log('done')

   );
  }
}     



   `I used the angular2 here no controller all are components, In this file i wrote registration page for my application'. 

The filed i added the drop for register person is enter with type as User or Admin or Supervisor, here i that drop down fields is not display when the registration page is loaded, it's show when we refresh already loaded page once again.

i attach the output images as follows: here the dropdown is not dsiplay after the companyName filed

And after i reload the page the dropdown is display (i.e) follows: the dropdown is comes after readload

  • can you show your controller side code ? also you are missing `<` in `option` check it – Pardeep Jain Aug 09 '17 at 11:36
  • 1
    Sry for < is missed but i already added in my code, i added the component code –  Aug 09 '17 at 12:46
  • your angular side is working fine, there must be error on css side, you need to inspect your code. and check whether code for dropdown exist in DOM or not – Pardeep Jain Aug 09 '17 at 13:42

1 Answers1

0

You need to fix the incorrect option element as per below:

<div class="form-group row">
<select id="select111" name="role"  placeholder="Roles" class="form-control selectpicker" style="width:476px;">
   <option *ngFor="let role of roles" [value]="role">{{role}}</option>
 </select>        

Also make sure the roles are being initialised in correctly in the controller typescript file.

See Angular 2 - Setting selected value on dropdown list

  • all the code is fine drop down is comes when second time i load or reload the registration page –  Aug 09 '17 at 12:59