builder.parse()与bindingContext

时间:2019-06-10 09:28:24

标签: nativescript angular-nativescript

我有以下代码:

  private loadSlides(): Promise<any> {
    return new Promise((resolve, reject) => {
      const component = builder.parse(slideTemplate);
      component.bindingContext = {
        content: 'HHAHAAHA'
      };
      resolve(component);
    });
  }

这是slideTemplate的值:

export const slideTemplate: string = `
  <GridLayout row="0" rows="*, 2*, *">
      <GridLayout width="57%" row="0" horizontalAlignment="center" verticalAlignment="center">
          <Label class="lobster-regular carousel-item-head" [text]="content" textWrap="true"></Label>
      </GridLayout>
  </GridLayout>
`;

现在,当我绑定模板中的值时,它仅显示空字符串。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

您不能在builder.parse(...)中使用Angular模板/绑定语法,因为它是在Angular上下文外部创建的。

由于您使用的是bindingContext(这是NativeScript Core处理数据绑定的方式),因此您应该使用text="{{ content }}"

export const slideTemplate: string = `
  <GridLayout row="0" rows="*, 2*, *">
      <GridLayout width="57%" row="0" horizontalAlignment="center" verticalAlignment="center">
          <Label class="lobster-regular carousel-item-head" text="{{ content }}" textWrap="true"></Label>
      </GridLayout>
  </GridLayout>
`;

我不确定为什么这里需要构建器,如果可能的话,我建议坚持使用Angular组件。