【问题标题】:How can i make custom Polymer element hidden?如何隐藏自定义聚合物元素?
【发布时间】:2013-12-29 21:37:38
【问题描述】:

对于实验,我使用来自 https://www.dartlang.org/docs/tutorials/polymer-intro/

的自定义元素 tute-stopwatch

当我为按钮设置属性'.hidden = true'时,这些按钮隐藏成功,但元素tute-stopwatch没有。

  void addChild(Event e, var detail, Node target) {
    ..
    stopButton.hidden = true;
    startButton.hidden = true;
    resetButton.hidden = true;

    this.hidden = true; 
}

当我在 tute_stop_watch.html 中使用子模板时:

<template if="{{ visible }}" id="innerTemplate">

在 tute_stop_watch.dart 中:

  void enteredView() {
    super.enteredView();
    startButton = $['startButton']; // $['startButton'] == null
    innerTemplate =$['innerTemplate'] // find correct, but innerTemplate.childNodes == []

我尝试让元素具有所有功能 tute-stopwatch 按需可见。

【问题讨论】:

    标签: dart dart-polymer


    【解决方案1】:

    将以下样式添加到您的聚合物元素中:

    <polymer-element name="tute-stopwatch">
      <template>
        <style>
          /* old style for current Dartium */
          @host {
            * {
              display: inline-block;
              position: relative;
              overflow: hidden;
            }
    
            [hidden], .hidden {
              display: none;
            }
          }
    
          /* new style for Chromium */
          :host {
            display: inline-block;
            position: relative;
            overflow: hidden;
          }
    
          :host([hidden]:host, .hidden:host) {
            display: none;
          }
        </style>
    
        <button>xxx</button>
        ...
    
      <template>
      <script type="application/dart" src='toute-stopwatch.dart'></script>
    </polymer-element>
    

    您可以通过设置hidden 属性或添加/删除类hidden 使元素可见/隐藏。

    【讨论】:

    • 谢谢! this.hidden 在这个魔法之后工作。我在哪里可以阅读更多关于 :host([hidden]:host, .hidden:host) 的信息,而且这仅在 Chromium 中有效?
    • Dartium 有点落后,但旧样式很快就会被替换。您可以找到更多关于此A Guide to Styling ElementsPolymer-Elements 的信息。不过,还有更多元素不是以“polymer_ui”开头的。
    • 感谢您的帮助。新年快乐!
    • Gunter,你的选择器是什么意思?什么是 [hidden]:host.hidden:host .... 这不适用于 :hover#myElement 所以我很好奇你的实现是做什么的,你能展示一个 jsfiddle 吗?
    • 呃,那是 2 岁,我不记得我曾经知道这件事。可能是一些被丢弃的实验性选择器)我想这就是 :host-context (html5rocks.com/en/tutorials/webcomponents/shadowdom-201/…) 现在的样子。
    【解决方案2】:

    您可以隐藏整个模板:

    <template>
        <template if="{{ visible }}">
         CONTENT
        </template>
    </template>
    

    visible 是一个 @published 布尔值。

    然后你可能在 dart 中有一个方法,例如:

    void hide() {
        visible = false;
    }
    

    您可以通过 HTML 中的点击链接调用它。

    【讨论】:

    • 谢谢!这是工作。但是当我使用子模板
    • 您似乎将飞镖代码放入模板中?也许你现在应该把它分成两个问题?
    • 对不起。这是我在这里的第一个问题。我在“tute_stop_watch.dart”中添加飞镖代码
    • 感谢您的帮助。新年快乐!
    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多