2016-12-16 80 views
0

的Javascript如何訪問Repeater內的外部元素?

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_roleRepeater。 任何人都可以幫助我如何訪問Repeater內的外部元素?

謝謝。

回答

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

+0

我'RadListView'在''直放站的母公司。這很重要嗎? –

+1

是的,那麼你需要使用$ parents ['Page']來訪問它。 $ parent只會上升到一個級別,無論綁定到RadListView。 – Nathanael

+0

猜猜這可以在獲取'user_role'時正常工作,但它無法評估'$ parents ['Page']。user_role ==='agency'的條件嗎? '可見':'collapse'' –