4

I'm trying to build an angular 2 application, my problem is that i want to give a default value for an input and make it disabled i tried this but it doesn't work.

<div class="form-group">
    <label for="input01">UUID</label>
    <input class="form-control form-control-rounded" id="input01" style="width:600px;margin-left:60px" type="text" [(ngModel)]="beacon.uuid" value="{{uuid}}" name="uuid" disabled>
  </div>

Any solution?

3
  • try placeholder = {{uuid}} Commented Apr 19, 2017 at 12:13
  • If you don't want to store default value in your model check this aswer Commented Apr 19, 2017 at 12:19
  • you should be setting this on the model. did you happen to read the docs...? - angular.io/guide/forms-overview Commented May 16, 2019 at 7:33

3 Answers 3

9

If you want to give a default value without two-way binding, use [value]:

<div class="form-group">
  <label for="input01">UUID</label>
  <input id="input01" type="text" [value]="beacon.uuid" class="form-control form-control-rounded" name="uuid" disabled>
</div>
Sign up to request clarification or add additional context in comments.

Comments

4

Your template:

<div class="form-group">
    <label for="input01">UUID</label>
    <input class="form-control form-control-rounded" id="input01" style="width:600px;margin-left:60px" type="text" [(ngModel)]="beacon.uuid" name="uuid" [disabled]="true">
  </div>

In your component:

ngOnInit() {
  beacon.uuid = "my default value"
}

Comments

0

Try removing the value from input

<div class="form-group">
    <label for="input01">UUID</label>
    <input class="form-control form-control-rounded" id="input01" type="text" [(ngModel)]="beacon.uuid" name="uuid" disabled>
</div>

Otherwise it will be a conflict between your style and disabled attribute

Comments

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.