0

I am using angular 4. Below is the problem.

I have one div inside that one span and one textbox. Whenever i enter something in the textbox and press enter. it has to create dynamically one span in the div and the size of the textbox has to be changed remining size. Below is the code which i have written in angular 4. The below code is not working because whenever i do array push it's not getting reflecting immediately. Please help me.

HTML CODE::

<div>
      <div id="searchBar" class="input-form">
        <label class="label-form">people In Topic</label>
        <div id="topicPeople" #topicPeopleSelcted style="border-bottom: 1px #e6e6e6;padding:0px;">
            <span style="padding:0px;">
                <span *ngFor="let person of topicPeoples" style="background-color: #e6e6e6;font-color:black;"> {{ person.name }}</span>
            </span>
            <textarea  (keydown.enter)="$event.preventDefault()" [style.width.px]="topicPeopleTextBoxWidth+'px'" rows="1" id="topicPeopleTextBox" formControlName="topicPeople"
                       style="border:none;" (keyup)="topicPeoplechange($event,topicPeopleHash.value)" #topicPeopleHash value="{{topicPeople}}"
                       spellcheck="false" autocomplete="false" autocapitalize="off" autocorrect="off" tabindex="1" dir="ltr" aria-autocomplete="list"></textarea>
        </div>
      </div>
    </div>

Type script code::

  import {
  ChangeDetectionStrategy, ChangeDetectorRef, Component, ElementRef, Input, OnInit, SimpleChanges,
  ViewChild
} from '@angular/core';
import {FormBuilder, Validators} from "@angular/forms";

@Component({
  templateUrl: './saveTopic.component.html',
  changeDetection : ChangeDetectionStrategy.OnPush,
  styleUrls: ['./../group/saveGroup.component.css','./../group/group.component.css']
})
export class SaveTopicComponent  implements OnInit{

  saveTopicForm : any;

  @Input() topicPeopleTextBoxWidth : number;

  peopleNoOfRows : number = 1;

  @ViewChild('topicPeopleSelcted') topicPeopleSelectedView: ElementRef;

  @Input() topicPeoples :   Array<Object>;

  constructor(private formBuilder: FormBuilder,private ref : ChangeDetectorRef) {
    this.topicPeoples = new Array();
    this.saveTopicForm = this.formBuilder.group({
      'topicName': ['', Validators.required],
      'topicDescription': ['', Validators.required],
      'topicPeople' : ['']
    });

   setInterval(() => {
        console.log("Testing");
        this.ref.markForCheck();
    },500)
  }
  ngOnInit() {
      this.topicPeopleTextBoxWidth = 420;
  }

  topicPeoplechange(event,inputValue) {
    if(event.keyCode == 13) {
      this.topicPeoples.push({
        "name": inputValue
      });
      console.log(this.topicPeopleSelectedView.nativeElement.offsetWidth);
      let childrens = this.topicPeopleSelectedView.nativeElement.children;
      let peopleDiv = childrens[0];
      let textBoxDiv =childrens[1];
      let peopleSpan = peopleDiv.children;
      let totalSpanWidth = 0;
      for (var _i = 0; _i < peopleSpan.length; _i++) {
        totalSpanWidth = totalSpanWidth + peopleSpan[_i].offsetWidth;
      }
      console.log(this.topicPeopleTextBoxWidth);
      console.log(totalSpanWidth);
      if(420 - totalSpanWidth > 0) {
          this.topicPeopleTextBoxWidth = 420 - totalSpanWidth;
      }
      console.log(this.topicPeopleTextBoxWidth);

    }
  }


  deepClone(oldArray: any[]) {
    let newArray: any = [];
    oldArray.forEach((item) => {
      newArray.push(item);
    });
    return newArray;
  }

}
AT82
  • 71,416
  • 24
  • 140
  • 167
amarnath
  • 1
  • 2
  • How does the `updatePeopleArray` get initialized? – Anthony C Oct 26 '17 at 18:17
  • What is the initial value of this.topicPeoples, have you initialized it? – TruckDriver Oct 26 '17 at 18:20
  • It's a method inside the class. i am not initializing it. I am just calling from topicPeopleChange method. – amarnath Oct 26 '17 at 18:27
  • @Input() topicPeoples : any[]; – amarnath Oct 26 '17 at 18:27
  • yes,, i have initialized it – amarnath Oct 26 '17 at 18:28
  • what error do you see in console? – TruckDriver Oct 26 '17 at 18:31
  • @amarnath and you are initializing topicPeoples to what value? this @Input() topicPeoples : any[]; should be @Input() topicPeoples : any[] =[]; – TruckDriver Oct 26 '17 at 18:32
  • Thanks for the quick reply.. I don't see any problems in the console. The problem here is in topicPeopleChange method i am push the data into the this.topicPeoples array , as the array changed it has create a span in the view. But the view is getting reflected after the method is getting completed. But i need to get it reflected as soon i update the array. so that i can get width on the element and do some operation on top of it. – amarnath Oct 26 '17 at 18:42

0 Answers0