Topic: Label overlaps input on two way binding

Er Ka free asked 6 years ago

Hello, I have a problem when implementing two way binding with ngModel. <divclass="md-form"> <inputtype="text"name="name"id="name"class="form-control" [(ngModel)]="" mdbActive #name="ngModel" required > <label for="name"class="active">Name</label> </div> My problem is label overlaps input and does not automatically goes up. Please suggest.  

stgiaf pro answered 6 years ago

Hello, I have the same issue. When the model is filled with observable, then the label does not come up when the value is actuallt filled by the observable. here is the typescript:
import { UserService } from './../user.service';
import { User } from './../models/user.type';
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
styleUrls: ['./user-card.component.scss']
export class UserCardComponent implements OnInit {
privateuser= {
department : {
id :0,
constructor(privateroute:ActivatedRoute, privaterouter:Router, privateuserService:UserService) {
route.params.subscribe ( p=> {['id'] ||0;
ngOnInit() {
u=> {
and here is the html
<divclass="md-form mt-5">
<i class="fa fa-user prefix gray-text"></i>
<input mdbActive type="text"id="name"name="name" [(ngModel)]="">
<label for="name">Your Name</label>
<div class="md-form">
<i class="fa fa-bullseye prefix gray-text"></i>
<inputmdbActivetype="text"id="department"name="department" [(ngModel)]="">
the result is as follows:
Can you please advise?

Damian Gemza staff commented 6 years ago

Hello stgiaf, Please try to add class Active to your's label elements. This should fix your's problem. Please inform me how situation looks like. Best Regards, Damian

stgiaf pro commented 6 years ago

Hi Damian, Thanks for your answer. I added class="active" to the label element., but it seems in the Inspection (Developer Tools in Chrome) that the class is removed somehow and the issue still persists. Please not that the page is opened via RouterLink. If I manually refresh the page, it works.

Damian Gemza staff commented 6 years ago

Dear stgiaf, could you send me your's project without node_modules folder at I tried to reconstruct your's problem but for me everything is working fine. I created a second component, and filled it with form. In inputs i've putted data via ngModel. Made route from main component to second component via button. When second component is loaded, inputs are filled with data, and label goes up. Best Regards, Damian

stgiaf pro commented 6 years ago

Hi, Sent via weTransfer

Adrian Sawicki free answered 6 years ago

If this is your html then you should start from cleaning it. Please check docs here:  based on this you should create what you want. If you won't be capable of doing it then come back. For now that what you show me is impossible to work with.  

Adrian Sawicki free answered 6 years ago

Okay, one by one. First of all please tell me what you want to do? If it's a normal input then that what I gave you is enough to connect it. If you want to use mdb-ng-select then please provide me more information.

Er Ka free commented 6 years ago

This is what I have in html: <div> <div><div class="md-form"></div> <div><input type="text"name="name"id="name"class="form-control"</div> <div>[(ngModel)]="" #name="ngModel" required [value]="" mdbActive></div> <div><label for="name"class="active">Name</label></div> <div><div class="alert alert-danger" *ngIf="f.submitted && name.invalid">Name is required</div></div> <div></div></div> <div></div> </div> My model is: item with name attribute

Adrian Sawicki free answered 6 years ago

I'm not sure how does your typescript part looks like so I will just give a working example.

In typescript:

myModel={name: 'test'};


<div class="md-form">
 <input type="text" name="name" id="name" class="form-control"
 [(ngModel)]="myModel" mdbActive value={{}} required >
 <label for="name"class="active">Name</label>

With something in the label goes up on page initialization.

Er Ka free commented 6 years ago

Name [(ngModel)]="myModel" => Doing like this I think not a correct way? Also binding does not work for mdb-ng-select component.

Please insert min. 20 characters.


Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.



Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No