如何访问Repeater中的外部元素?

时间:2016-12-16 05:07:14

标签: javascript xml observable nativescript telerik-appbuilder

的Javascript

var pageData = new Observable({
    appointmentsList: appointmentsList,
    user_role: ''
});

exports.onLoaded = function (args) {
    page = args.object;
    page.bindingContext = pageData;
    pageData.set('user_role', appSettings.getString('role'));

XML

<Repeater items="{{appointmentsList.booking_dates.bookings}}">
    <Repeater.itemsLayout>
        <StackLayout class="appointment-rows" />
    </Repeater.itemsLayout>
    <Repeater.itemTemplate>
        <StackLayout>
            <WrapLayout visibility="{{ user_role === 'agency' ? 'visible' : 'collapse' }}">
                <Label text="{{user_name}}" class="row-client-name row-client-name-new" />
            </WrapLayout>
        </StackLayout>
    </Repeater.itemTemplate>
 </Repeater>

在上面的代码段中,问题是我无法访问user_role内的Repeater。 任何人都可以帮助我如何访问Repeater内的外部元素吗?

感谢。

2 个答案:

答案 0 :(得分:3)

根据此https://docs.nativescript.org/core-concepts/data-binding#example-4-creating-listview-child-items-based-on-the-itemtemplate,你可以尝试这样的事情:

<WrapLayout visibility="{{$parents['Repeater'].user_role,$parents['Repeater'].user_role === 'agency' ? 'visibile' : 'collapse' }}">

或者这样

<WrapLayout visibility="{{$parents['Page'].user_role,$parents['Page'].user_role === 'agency' ? 'visibile' : 'collapse' }}">

答案 1 :(得分:1)

您需要使用$parent$parents[]属性。

我相信:

<WrapLayout visibility="{{ $parent.user_role, $parent.user_role === 'agency' ? 'visible' : 'collapse' }}">

应该有效。然而,由于这是在转发器中,我不相信它对父母绑定做了任何奇怪的事情;但如果这不起作用 - 你可以简单地这样做: <WrapLayout visibility="{{ $parents['Page'].user_role, $parents['Page'].user_role === 'agency' ? 'visible' : 'collapse' }}">

此功能的NativeScript文档:http://docs.nativescript.org/core-concepts/data-binding#binding-to-a-parent-binding-context