【问题标题】:Polymer 1.0 data binding not workingPolymer 1.0 数据绑定不起作用
【发布时间】:2015-06-16 23:37:23
【问题描述】:

我有以下聚合物元素:

调用 someMethod 后,navigator.currentStep 的值没有更新。

<dom-module id="m">
  <template>
    Navigator step = <span>{{navigator.currentStep}}</span>
  </template>
</dom-module>


Polymer({
        is: 'm',
        ready: function() {
          this.navigator = new Navigator(1);
          console.log(this.navigator.currentStep);  // 1
        },
        someMethod: function() {
          this.navigator.next();
          console.log(this.navigator.currentStep);   // 2
}
});

输出总是

导航步骤 = 1

但以下工作

<dom-module id="m">
  <template>
    Navigator step = <span>{{currentStep}}</span>
  </template>
</dom-module>


Polymer({
        is: 'm',
        ready: function() {
          this.navigator = new Navigator(1);
          this.currentStep = this.navigator.currentStep;   // 1
        },
        someMethod: function() {
          this.navigator.next();
          this.currentStep = this.navigator.currentStep;   // 2
}
});

【问题讨论】:

    标签: binding polymer


    【解决方案1】:

    致电this.notifyPath('navigator.currentStep', this.navigator.currentStep)

    https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#set-path

    有时命令式代码需要直接更改对象的子属性。由于我们避免使用更复杂的观察机制,例如 Object.observe 或脏检查,以便为最常见的用例实现跨平台的最佳启动和运行时性能,因此直接更改对象的子属性需要用户的配合。

    具体来说,Polymer 提供了两个 API,允许将此类更改通知系统:notifyPath(path, value)set(path, value),其中 path 是标识路径的字符串(相对于宿主元素)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多