简体   繁体   中英

Knockout Js - Using with inside if condition

The isAdmin property inside foreach is related in authorization class.

I am getting an error saying isAdmin is not defined. Is it because I am using isAdmin in foreach ? How can I use a use property in with in foreach .

<div data-bind="with: authorization">
<table>
<tbody data-bind="foreach: users">
<tr>
    <td data-bind="text: id"></td>
    <td data-bind="text: name"></td>
    <!-- ko if: isAdmin -->
    <td><a href="#" class="icon-trash" rel="tooltip" title="Delete" ></a></td>
    <!-- /ko -->
</tr>
</tbody>
</table>

You are correct, it is not working because the foreach binding gets its own context. Use $parent to access the parent authorization context.

<!-- ko if: $parent.isAdmin -->
<td><a href="#" class="icon-trash" rel="tooltip" title="Delete" ></a></td>
<!-- /ko -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM