如何在 ember 中仅通过一种方式绑定/完全不绑定将数据传递给组件?

How to pass data to component with one way binding only / without binding at all in ember?

在模板中,我将组件调用为:

{{comp-name data=controllerData}}

如果我更改组件中的数据,控制器的 controllerData 也会因隐式双向绑定而发生更改。我有两个问题:

1) 如何使其成为单向绑定。这样只有来自控制器 controllerData 的更改才会传播到组件的数据。

2) 根本不应该有绑定。意思是,如果我更改组件或控制器中的数据。它不应该反映在其他事情上。

1) 使用 readonly 助手:

{{comp-name data=(readonly controllerData)}}

2) 使用 unbound 助手:

{{comp-name data=(unbound controllerData)}}

有关详细信息,请参阅 http://emberup.co/bindings-with-htmlbars-helpers/

在带有尖括号组件的 Ember 的未来版本中,绑定将默认为一种方式,您将需要使用 mut 帮助程序使其成为两种方式。

指南中的这篇文章应该对您有所帮助:

https://guides.emberjs.com/v2.18.0/object-model/bindings/

它给出了这个例子:

import EmberObject, { computed } from '@ember/object';
import Component from '@ember/component';
import { oneWay } from '@ember/object/computed';

user = EmberObject.create({
  fullName: 'Kara Gates'
});

UserComponent = Component.extend({
  userName: oneWay('user.fullName')
});

userComponent = UserComponent.create({
  user: user
});

// Changing the name of the user object changes
// the value on the view.
user.set('fullName', 'Krang Gates');
// userComponent.userName will become "Krang Gates"

// ...but changes to the view don't make it back to
// the object.
userComponent.set('userName', 'Truckasaurus Gates');
user.get('fullName'); // "Krang Gates"

希望对您有所帮助。