2

I have some forms that should be editable only by some users. Other users should only be able to view it.

Currently, for each form control in a form, I'm checking condition and displaying either a form control or a text. Something like,

<div>
    <input type="text" *ngIf="editable">
    <p *ngIf="!editable">{{value}}</p>
</div>

This doesn't seem to be an elegant way. Please suggest a cleaner way to achieve this, like doing something at the top level.

1
  • 1
    Why can't you make 2 different views (readonly and editable) rather than toggling each control separately? Commented Nov 15, 2017 at 14:37

2 Answers 2

4

Since you are using Angular, it is better to use FormControls and FormGroups. Take a look at this guide. Using the FormControl API you can enable and disable the input programmatically.

An other way is to use property binding and do something like this:

<div>
   <input type="text" [readonly]="!editable">
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

No. I don't wanna use readonly or disabled attribute. I want to show a static element instead of a form control like select or input, but without toggling each control individually.
I am not sure that I understand what you mean. If you style your input to look like a "static" element?
Angular doesn't encourage binding to disabled elements. I can't use readonly as my forms have select element and readonly doesn't have any effect on them. Moreover, I primarily don't want to toggle each element individually.
-1

I think you should take a look to the ng-disabled directive. This should do the trick here!

2 Comments

Your link is for Angular 1.x and the questions source is Angular 2+.
My bad srry. The answer was given here : stackoverflow.com/questions/37159827/…

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.