0

I am struggling with ng-show and $invalid logic, my requirement is very simple I need to make a form that will show input fields based on conditions.

I need to add one datetime picker of moment.js.

Issue starts when I understand that my datetime picker is not working with ng-if because ng-if removes the element from the DOM and my datetime picker can't work with this, I used ng-show instead and it worked. But the issue is my form is divided into multiple different forms based on conditions I am showing different inputs and datetime picker is not in all the conditions.

This thing causing my submit button to kept disabled on those conditions when I am not showing datetime picker.

I tried finding a lot thought of using ng-switch as well but I got to know that ng-swith also works the same as ng-if, this will again cause me the same issue if datetime picker (won't work).

Is there any way to resolve this

My code structure -

<div id="mainDiv">
    <form id="myForm">

        <div ng-show="condition1">

            <div ng-if="condition1of1">
             // input fields such as text email password
            </div>

            <div ng-show="condition2of1">
            //date time picker here 
            </div>
            
            <div ng-if="condition3of1">
            // other input fields
            </div>

        </div>

        <div ng-if="condition2>......</div>

        <div ng-if="condition3>......</div>

        <input id="btn" type="submit" ng-disabled="myForm.$invalid" value="Submit" ng-click="submitData()"> // button that will remain disable until all the required fields will not get resolved.
    </form>
</div>

How to resolve this issue?

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
Rikk Mor
  • 65
  • 12
  • maybe you could create a directive that houses your datepicker - that directive would also initialize the picker functionality. So when you use ng-if it should still work. Just a guess – Kinglish May 04 '22 at 16:28
  • Thank you suggestion, I actually created a simple function and called it in ng-init with ng-if this resolved the date picker initialization issue. – Rikk Mor May 11 '22 at 16:23

0 Answers0