嵌套元素中的Dartlang聚合物点击事件

时间:2016-05-24 12:57:25

标签: dart polymer dart-polymer

我正试图让onclick事件发生。我的代码如下所示:

的index.html:     

<!--                                                                                                                                                                                                               
  Copyright (c) 2016, <your name>. All rights reserved. Use of this source code                                                                                                                                    
  is governed by a BSD-style license that can be found in the LICENSE file.                                                                                                                                          
-->

<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">   
  <meta name="scaffolded-by" content="https://github.com/google/stagehand">
  <title>polymer_test_project_2</title>

  <!-- Add to homescreen for Chrome on Android -->  
  <meta name="mobile-web-app-capable" content="yes">
  <link rel="icon" sizes="192x192" href="images/touch/chrome-touch-icon-192x192.png">

  <!-- Add to homescreen for Safari on iOS -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Web Starter Kit">
  <link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">

  <!-- Tile icon for Win8 (144x144 + tile color) -->
  <meta name="msapplication-TileImage" content="images/touch/ms-touch-icon-144x144-precomposed.png">
  <meta name="msapplication-TileColor" content="#3372DF">

  <!--  Polyfill of Custom Elements and HTML Imports -->
  <script src="packages/web_components/webcomponents-lite.min.js"></script>

  <script defer type="application/dart" src="index.dart"></script>
  <script defer src="packages/browser/dart.js"></script>

  <!-- example of using a paper element -->
  <link rel="import" href="packages/polymer_elements/roboto.html">

  <link rel="stylesheet" href="styles.css">
</head>

<body unresolved>
  <main-app></main-app>
</body>
</html>

index.dart:

// Copyright (c) 2016, <your name>. All rights reserved. Use of this source code                                                                                                                                   
// is governed by a BSD-style license that can be found in the LICENSE file.                                                                                                                                       
library my_project.web.index;

import 'package:polymer_test_project_2/main_app.dart';
import 'package:polymer/polymer.dart';

/// [MainApp] used!                                                                                                                                                                                                
main() async {
  await initPolymer();
}

main_app.html:

<!--                                                                                                                                                                                                               
  Copyright (c) 2016, <your name>. All rights reserved. Use of this source code                                                                                                                                    

由BSD风格的许可证管理,该许可证可在LICENSE文件中找到        - &GT;     

<dom-module id="main-app">
  <style>
    :host {
      display: block;
    }
  </style>

  <template>
    <paper-input label="Type something..." value="{{text}}"></paper-input>
    <p>
      Text: <span>{{text}}</span><br />
      Reversed: <span>{{reverseText(text)}}</span>
    </p>
    <my-component>
    </my-component>
  </template>
</dom-module>

main_app.dart:

@HtmlImport('main_app.html')

library polymer_test_project_2;
import 'dart:html';

import 'package:polymer_elements/paper_input.dart';
import 'package:polymer_elements/paper_button.dart';
import 'package:polymer/polymer.dart';
import 'package:web_components/web_components.dart';

part 'my_component.dart';

/// Uses [PaperInput]                                                                                                                                                                                              
@PolymerRegister('main-app')
class MainApp extends PolymerElement {
  @property
  String text;

  MyComponent myComponent;

  /// Constructor used to create instance of MainApp.                                                                                                                                                              
  MainApp.created() : super.created();

  @reflectable
  String reverseText(String text) {
    return text.split('').reversed.join('');
  }
}

my_component.html:

<dom-module id="my-component">
  <template>
    <span>foobar</span>
    <span>{{text}}</span>
    <paper-button on-click="{{ settext }}">setText</paper-button>
  </template>
</dom-module>

和my_component.dart:

@HtmlImport( 'my_component.html') polymer_test_project_2的一部分;

@PolymerRegister('my-component')
class MyComponent extends PolymerElement {
  @property
  String text="hello";

  MyComponent.created() : super.created() {
    print("mycomponent created");
  }

  @Listen('settext')
    void setText([_, __]) {
    print("text set to goodbye");
    text="goodbye";
  }
}

目前,该按钮显示在我的组件中,但是当我点击该按钮时,会出现错误消息:

[my-component::_createEventHandler]: listener method `{{ settext }}` not defined

有人知道如何让它发挥作用吗? :)

非常感谢提前!

1 个答案:

答案 0 :(得分:1)

对于事件绑定,请勿使用{{...}},这仅适用于值绑定。 on-已经指示聚合物事件绑定,事件绑定只允许在任何地方使用函数名称,因此只需要这样做:

<paper-button on-click="settext">setText</paper-button>