2
<input pInputText type="text" formControlName="description" class="form-control" placeholder="Required" *ngIf="pageModes[formModel.schema.mode] === pageModes[pageModes.View] />

Above is the input box which I used.. My condition:

IF(pageModes[formModel.schema.mode] === pageModes[pageModes.View])
{
    -----------(disable the input box)-------------------
}
ELSE
{
    -----------(enable the input box)-------------------
}

When I simply use the condition pageModes[formModel.schema.mode] === pageModes[pageModes.View], the input box got invisible.

Can anybody help me to disable the input box using the condition "pageModes[formModel.schema.mode] === pageModes[pageModes.View]"

Jojofoulk
  • 2,127
  • 1
  • 7
  • 25
Angel Reji
  • 533
  • 2
  • 11
  • 26
  • You need to disable the corresponding FormControl in your TypeScript code: https://angular.io/api/forms/AbstractControl#disable – JB Nizet May 09 '19 at 07:15
  • When I used this, input got invisible.. I need to disable the input – Angel Reji May 09 '19 at 07:20
  • Possible duplicate of [Disable input based on a condition](https://stackoverflow.com/questions/51714933/disable-input-based-on-a-condition) – ikramf90 May 09 '19 at 07:36

3 Answers3

1

You could use [disabled]="*** your condition ***" instead. Every html attribute on input elemets can be binded by using [attribute].

In your case this would be:

<input pInputText 
    type="text" 
    formControlName="description" 
    class="form-control" 
    placeholder="Required" 
    [disabled]="pageModes[formModel.schema.mode] === pageModes[pageModes.View] />

You could also use the formControlName and set disabled in the controller.

Jonas Johansson
  • 417
  • 3
  • 8
  • I have multiple conditions. disable functioning need to work only when the condition, "pageModes[formModel.schema.mode] === pageModes[pageModes.View]" happens. When I used the condition which you told, it affects to all conditions. – Angel Reji May 09 '19 at 07:23
  • Thank you for your reply.. I used [attr.disabled] = "pageModes[formModel.schema.mode] === pageModes[pageModes.View] ? 'disabled' : null".. And now its working – Angel Reji May 09 '19 at 08:23
  • You're welcome. Together we can learn more so I'm happy to help. You shouldn't need to use attr.disabled though. disabled should be enough. Feel free to accept or upvote the answer :) – Jonas Johansson May 09 '19 at 09:16
0

Remove your *ngIf and add disabled to your formControl:

// Whereever you're building your form:
description: new FormControl({value: '', disabled: pageModes[formModel.schema.mode] === pageModes[pageModes.View]})
Roberto Zvjerković
  • 9,657
  • 4
  • 26
  • 47
-1

add inputDisabled Property into your component

export class Component{
   get inputDisabled(){
    return // add your disable logic here 
   }
}

set this property to input attribute

<input [disabled]="inputDisabled"  .../>
onik
  • 1,579
  • 1
  • 11
  • 19