首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在polymer.dart中绑定表

在polymer.dart中绑定表
EN

Stack Overflow用户
提问于 2013-08-14 08:53:45
回答 3查看 1.8K关注 0票数 4

如果不使用自定义元素,则无法将模型绑定到表。自定义元素工作正常,但当我尝试手动绑定模型而不创建自定义组件时,不会显示任何数据。有什么想法吗?

custom_table.html

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <body>    
    <polymer-element name="custom-table" extends="div" apply-author-styles>            
      <template>      
        <table>
          <tbody>
            <tr template repeat="{{people}}">
              <td>{{firstName}}</td>
              <td>{{lastName}}</td>
            </tr>
          </tbody>
        </table>
      </template>      
      <script type="application/dart" src="custom_table.dart"></script>
    </polymer-element>   
  </body>
</html>

custom_table.dart

代码语言:javascript
复制
import 'package:polymer/polymer.dart';

class Person  {
  String firstName;
  String lastName;        
  Person(this.firstName, this.lastName);
}

@CustomTag('custom-table')
class CustomTable extends PolymerElement with ObservableMixin {
  List people = [new Person('Bob', 'Smith'), new Person('Alice', 'Johnson')];
}

没有自定义元素版本:

polymer_test.html

代码语言:javascript
复制
<!DOCTYPE html>
<head>        
    <script src="packages/polymer/boot.js"></script>
</head>
<html>
  <head>
    <meta charset="utf-8">
    <title>Polymer test</title>
    <link rel="stylesheet" href="polymer_test.css">
  </head>
  <body>   
   <h1> Table test </h1>
    <template id="tableTemplate" repeat>
      <table>       
        <tbody>
          <tr template repeat="{{ people }}">
            <td>{{firstName}}</td>
            <td>{{lastName}}</td>
          <tr>            
        </tbody>  
      </table>
    </template>    
    <script type="application/dart" src="polymer_test.dart"></script>    
  </body>
</html>

polymer_test.dart

代码语言:javascript
复制
import 'dart:html';
import 'package:polymer/polymer.dart';
import 'package:fancy_syntax/syntax.dart';

class Person {
  String firstName;
  String lastName;        
  Person(this.firstName, this.lastName);
}

class Message extends Object with ObservableMixin {  
  List<Person> people = toObservable([new Person('Bob', 'Smith'), new Person('Alice', 'Johnson')]);
}

main() {
  var msgModel = new Message();  
  TemplateElement template = query('#tableTemplate');
  template.bindingDelegate = new FancySyntax();
  template.model = msgModel;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-10 12:57:02

在我的模板定义中缺少绑定属性。相反,我错误地重复了。它应该是:

代码语言:javascript
复制
<template id="tableTemplate" bind>

而不是:

代码语言:javascript
复制
<template id="tableTemplate" repeat>
票数 0
EN

Stack Overflow用户

发布于 2013-08-14 19:15:10

把这个添加到main中有帮助吗?

代码语言:javascript
复制
void main() {
  initPolymer([], () {
    // put existing main code in here
  });
}

polymer/boot.js应该能做到这一点,但我认为,除非页面中至少有一个聚合物元素(这是一个bug),否则它不会出现。

或者,您可以向HTML中添加一个虚拟的<polymer-element>。这样就能解决问题。

票数 1
EN

Stack Overflow用户

发布于 2013-11-07 05:41:18

你能试试吗

代码语言:javascript
复制
templateBind(query('#tableTemplate'))
    ..bindingDelegate = new FancySyntax()
    ..model = msgModel;

请参阅binding.html#templateBind

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18227193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档