V
V
v-orlov2016-08-10 19:14:08
Angular
v-orlov, 2016-08-10 19:14:08

How to display value in block using Angular js if checkbox is selected?

good evening. i started learning angularjs and am stuck on the simplest exercises. for example, how to display the values ​​of the selected checkboxes in the string myResult separated by commas? Do I need to create a new controller or can I get by with some directives? what is the best way to arrange it? When do you need to create controllers at all?

<div class="input-checkbox">
  <input class="checkbox" id="id1" type="checkbox" name="id1" ng-model="itemChecked1" value="Часы">
  <label for="id1">Часы</label>
</div>
<div class="input-checkbox">
  <input class="checkbox" id="id2" type="checkbox" name="id2" ng-model="itemChecked2" value="Блокнот">
  <label for="id2">Блокнот</label>
</div>
<div class="input-checkbox">
  <input class="checkbox" id="id3" type="checkbox" name="id3" ng-model="itemChecked3" value="Подушка">
  <label for="id3">Подушка</label>
</div>

<div class="myResult">Вы выбрали: {{ }}</div>

thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DTX, 2016-08-10
@DirecTwiX

The controller is for the page.
There you create a fourth variable for result, which you update when updating the checkboxes:
result = itemChecked1 + ', '+itemChecked2+', '+itemChecked3
In the markup,
you have selected: {{result}}
You can try without a variable:
You have selected: {{itemChecked1 + ', '+itemChecked2+', '+itemChecked3
}}
The point should be clear
embed.plnkr.co/g0NMG4rmF4uwzoG2uZhf/preview
embed.plnkr.co/PXpGyS3mPNvlEfjPodOl/preview

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question