0

I am working on angular2 application.

I am getting one issue when I am appending date field from typescript the field was generated and looking perfect but the functionality I have performed on field click was not working(date picker will open on click on field and event icon).

HTML

<div id="specific_dates_container" class="field_area">
</div>

TYPESCRIPT

let count = document.querySelectorAll('.specific-date').length;
count += 1;
this.addCouponForm.addControl('specific_date'+count, new FormControl());

let html = "<div class='specific-date' id='specific_date"+count+"'><input placeholder='MM/DD/YYYY'  ngx-mydatepicker [options]='calendarOptions' #sd"+count+"='ngx-mydatepicker' formControlName = 'specific_date"+count+"' (click)='sd"+count+".toggleCalendar()' readonly><i class='material-icons' (click)='sd"+count+".toggleCalendar()'>event</i></div><br>";

$("#specific_dates_container").append(html);
1
  • 1
    Generally, you should avoid working with DOM using Angular especially with jQuery. Maybe try using *ngIf directive to conditionally append or hide some fields Commented Jul 13, 2017 at 9:36

1 Answer 1

0

You need to change your approach towards this

It seems you are tying to create controls dynamically To do that you need to follow this link

How to dynamically add and remove form fields in Angular 2

Sign up to request clarification or add additional context in comments.

5 Comments

I am not getting any issue with form fields they are working properly but the functionality I have implemented on HTML tags is not working. Ex : Click function.
Yes It will not work as Angular creates it's own virtual dome and binds its own events wile initializing components.. For example if using Jquery you try to add some element in dom with directive property binding then it will not work as Angular virtual dom does not know about that Because this binding happens on compile time not on runtime
Can you please suggest me any source from where I will understand what you are saying.
Your question is possible duplicate of following stackoverflow.com/questions/41231521/…
This might help you understanding why I am saying this. Though it talks about ng 1. But good thing is to use best practices stackoverflow.com/questions/30007792/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.