【问题标题】:What are the scoping rules of a `<template repeat>``<template repeat>` 的作用域规则是什么
【发布时间】:2015-04-15 17:44:32
【问题描述】:

在模板 repeat 内创建的绑定是否需要聚合物元素上的关联属性,我认为它们绑定到 ObservableList 的数组项?

在我的模板中是以下代码:

<polymer-element name="event-details">
  <template>
    <!-- etc -->
    <template repeat="{{artist in event.artist}}">
      <artist-card artist="{{artist}}">
    </template>
    <!-- etc -->
  </template>
  <!-- etc -->
</polymer-element>

我的元素定义看起来像

@CustomTag('event-details')
class EventDetails {
   /* etc */
   @published
   api.Event get event => readValue(#event);
   set event(api.Event event) => writeValue(#event, value);
   /* etc */
}

当我创建元素时,我得到了错误

"EventDetails has no attribute `artist`"

但我认为&lt;template repeat&gt; 中的artist 应该只存在于该模板的范围内。在元素的定义上使用{{artist}} 没有任何价值,因为它只会指向模板子项中的最后一个元素。

artistsapi.Event 对象上的ObservableList,尽管它是在不使用聚合物的情况下定义的。

【问题讨论】:

  • 能否尝试将局部变量重命名为repeat="{{a in event.artist}}"artist="{{a}}"
  • Error evaluating expression 'a': Class 'EventDetails' has no instance setter 'a='.
  • 接下来我会尝试一个简单的字段@published api.Event event;,而不是 readValue/writeValue。
  • 不,仍然出现同样的错误。
  • 将其更改为 &lt;template repeat="{{a in event.artists}}"&gt;{{a}}&lt;/template&gt; 会消除错误,因此它可能与 artistartist-card 中是 @published 的事实有关。

标签: dart polymer dart-polymer


【解决方案1】:

使用简单的属性(field/getter/setter)代替 readValue/writeValue。

【讨论】:

  • 这是一个解决方案,但不是特别令人满意。我遇到了绑定不更新过去简单属性的值的问题(特别是在处理&lt;input&gt;s 时),我发现 readValue/writeValue 通常可以解决我遇到的任何问题,所以我默认一切。不过我会提交那个错误。
猜你喜欢
  • 2018-04-19
  • 2023-01-14
  • 2011-12-01
  • 2018-01-26
  • 1970-01-01
  • 2017-05-23
  • 2011-08-23
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多