在poly.dart中,如果希望将控制器中定义的变量公开给视图端,可以用@observable定义该变量,并使用双胡子嵌入该变量.但是,一些文档和教程使用@published来满足相同的目的.官方文档也同时使用这两种方法,所以我不认为@published是定义变量的遗留的和不推荐使用的方式.

那么这两者有什么不同呢?我应该在我的代码中使用哪一个呢?

推荐答案

@published - is two way binding ( model to view and view to model)

如果模型属性也是标记中的属性,@published的用例是.

示例:对于要从外部源提供数据的表元素,您将定义属性数据,在本例中,data property应为@Publisher.

<polymer-element name = "table-element" attributes ="structure data">
     <template>
         <table class="ui table segment" id="table_element">
             <tr>
                 <th template repeat="{{col in cols}}">
                     {{col}}
                 </th>
             </tr>
             <tr template repeat="{{row in data}}">
              etc......
     </template>
<script type="application/dart" src="table_element.dart"></script>
</polymer-element>


@CustomTag("table-element")
class Table extends PolymerElement {

 @published  List<Map<String,String>>  structure; // table struture column name and value factory
 @published List<dynamic> data; // table data

@observable - is one way binding - ( model to view)

如果您只想将数据从模型传递到视图,请使用@Observable

示例:要使用上面的表格元素,我必须提供数据,在这种情况下,数据和 struct 将在我的表格测试dart 代码中可见.

<polymer-element name = "table-test">
 <template>
     <search-box data ="{{data}}" ></search-box>
     <table-element structure ="{{structure}}" data = "{{data}}" ></table-element>
 </template>
<script type="application/dart" src="table_test.dart"></script>
</polymer-element>

省道代码

CustomTag("table-test")
class Test extends PolymerElement {

  @observable List<People> data = toObservable([new People("chandra","<a href=\"http://google.com\"  target=\"_blank\"> kode</a>"), new People("ravi","kiran"),new People("justin","mat")]);
  @observable List<Map<String,String>> structure = [{ "columnName" : "First Name", "fieldName" : "fname"},
                                                     {"columnName" : "Last Name", "fieldName" : "lname","cellFactory" :"html"}];
  Test.created():super.created();

例子取自My Repo

Dart相关问答推荐

如何扩展 Dart 套接字 BroadcastStream 缓冲区大小为 1024 字节?

无法在 IOS 上使用 firebase 运行Flutter应用程序

Flutter:是否可以有多个 Futurebuilder 或一个 Futurebuilder 用于多个 Future 方法?

Flutter 使用拖动和按钮单击扩展 TextField

执行 `dart2js` 时会生成哪些文件?为什么?

如何等待forEach完成异步回调?

dart:类构造函数被标记为const是什么意思

如何在Flatter中的屏幕中心创建选项卡栏?

Flutter中不能指定MultiPartFile的内容类型

如何 Select 性地传递可选参数?

如何在 Dart 中获取当前脚本的目录?

Dart null 安全性不适用于类字段

在 Dart 中,将动态转换为给定类型或返回 null 的语法好方法?

在 Dart 中创建具有可变数量的参数或参数的函数

如何在 base64 中编码 Dart 字符串?

如何在 Dart 中为 Completer.CompleteException(exception, stackTrace) 获取当前堆栈跟踪;

在 Dart 中覆盖哈希码的好方法是什么?

如何展平flatten列表?

如何比较 Dart 中的列表是否相等?

如何用 Dart 格式化日期?