Aurelia是否有AngularJS $ watch替代品?

埃戈尔·马尔科维奇

我正在尝试将当前的Angular.js项目迁移到Aurelia.js。我正在尝试做这样的事情:

report.js

export class Report {
       list = [];

       //TODO
       listChanged(newList, oldList){
             enter code here
       }
}

report.html

<template>
    <require from="component"></require>
    <component list.bind="list"></component>
</template>

所以问题是:如何检测列表何时更改?

在Angular.js中,我可以做

$scope.$watchCollection('list', (newVal, oldVal)=>{ my code });

也许奥雷利亚有类似的东西吗?

亚历山大·米哈申科(Alexander Mikhalchenko)

对于@bindable字段,listChanged(newValue, oldValue)只要list更新值就会调用该字段请看看Aurelia文档

@customAttribute('if')
@templateController
export class If {
  constructor(viewFactory, viewSlot){
    //
  }

  valueChanged(newValue, oldValue){
    //
  }
}

您还可以使用ObserveLocator在奥里利亚笔者的博文中描述在这里

import {ObserverLocator} from 'aurelia-binding';  // or 'aurelia-framework'

@inject(ObserverLocator)
class Foo {  
  constructor(observerLocator) {
    // the property we'll observe:
    this.bar = 'baz';

    // subscribe to the "bar" property's changes:
    var subscription = this.observerLocator
      .getObserver(this, 'bar')
      .subscribe(this.onChange);
  }

  onChange(newValue, oldValue) {
    alert(`bar changed from ${oldValue} to ${newValue}`);
  }
}

UPD

正如Jeremy Danyow这个问题中提到的

ObserverLocator是Aurelia内部的“裸机” API。现在有一个可用于绑定引擎的公共API:

import {BindingEngine} from 'aurelia-binding'; // or from 'aurelia-framework'

@inject(BindingEngine)
export class ViewModel {
  constructor(bindingEngine) {
    this.obj = { foo: 'bar' };

    // subscribe
    let subscription = bindingEngine.propertyObserver(this.obj, 'foo')
      .subscribe((newValue, oldValue) => console.log(newValue));

    // unsubscribe
    subscription.dispose();
  }
}

最好的问候,亚历山大

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是否有替代品供Android的testflight使用

来自分类Dev

gitbash有什么好的替代品?

来自分类Dev

是否有DynamicResource的替代品

来自分类Dev

已取代的deferred.callback是否有替代品?

来自分类Dev

Cakephp中是否有邻居的替代品

来自分类Dev

改造2中的RequestInterceptor是否有替代品?

来自分类Dev

Aurelia是否有ng-template替代品?

来自分类Dev

Kotlin拉链所有替代品

来自分类Dev

是否有JDK 11的替代品库CORBA

来自分类Dev

Tensorflow 2.0中是否有cudnnLSTM或cudNNGRU替代品

来自分类Dev

aspnetboilerplate IIocResolver.ResolveAll是否有替代品?

来自分类Dev

有其他替代品吗?

来自分类Dev

angularJS是JSF的替代品吗?

来自分类Dev

有Adobe Flash的替代品吗?

来自分类Dev

Ubuntu One的替代品有哪些?

来自分类Dev

是否有ICQ的替代品?

来自分类Dev

有可用的GarageBand替代品吗?

来自分类Dev

AngularJS $ watch的替代品,可从服务中触发用户界面更改

来自分类Dev

GNU ld的替代品有哪些?

来自分类Dev

是否有DynamicResource的替代品

来自分类Dev

栅栏有替代品吗?

来自分类Dev

Ubuntu终端有替代品吗?

来自分类Dev

是否可以接受WinExec()的替代品?

来自分类Dev

在AngularJs中,有没有比$ watch更高性能的替代品?

来自分类Dev

有Unity替代品吗?

来自分类Dev

LibreOffice是否有替代品?

来自分类Dev

$ watch的替代品

来自分类Dev

有Android的ViewOverlay替代品吗?

来自分类Dev

swpackage的替代品有哪些?