4

I am new to KO, in my html file I am printing a value like this -

<!-- ko foreach: { data: JSON.parse($parent.options), as: 'option' } -->
 <!-- ko if: option.label === 'AAA' || option.label === 'BBB' -->
 <dd class="values" data-bind="html: option.value"></dd>
 <!-- /ko --> 
<!-- /ko -->

And its working fine . But what I want is, I want assign all values of loop to a variable and want to print after loop. Like we do in php-

foreach($data as $key=> $index){
 if($key==0)
 $var = $index['value'];
 else
 $var .= ' '.$index['value']; 
}
echo $var

Want to do the same in KO's html file using above KO loop.

Please help me in this.

Thanks

Vivek Kumar
5,7932 gold badges26 silver badges55 bronze badges
asked Feb 27, 2019 at 15:04

1 Answer 1

0

You can use ifnot binding to accomplish this as follows ;

<!-- ko foreach: { data: JSON.parse($parent.options), as: 'option' } -->
 <!-- ko if: option.label === 'AAA' || option.label === 'BBB' -->
 <dd class="values" data-bind="html: option.value"></dd>
 <!-- /ko -->
 <!-- ko ifnot: option.label === 'AAA' || option.label === 'BBB' -->
 <dd class="values" data-bind="html: ' ' + option.value"></dd>
 <!-- /ko -->
<!-- /ko -->
answered Jun 21, 2019 at 7:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.