【问题标题】:How to manually get/set object model from AngularDart component?如何从 AngularDart 组件手动获取/设置对象模型?
【发布时间】:2014-12-09 22:14:24
【问题描述】:

Angular 在 Scope 中的某处管理我们的对象,但我需要手动设置和获取控制组件的对象,这样当我通过 JSON 从 Web 下载其模型时,我可以自动设置所有组件的信息。

示例

@Component (selector: 'my-component')
class MyComponent {
    //component fields
}

当用户与<my-component> 交互时,它会更改其字段。我希望能够获取整个 MyComponent 对象及其字段以像这样保存它们

MyComponent comp = magicComponentGetterFunc('my-component');
String json = encodeJson (comp); //I do this this redstone_mapper

【问题讨论】:

  • 您能否添加有关您尝试完成的任务的更多详细信息(一些代码)。
  • @GünterZöchbauer 查看新示例 :)
  • 所以你不想搜索 DOM 然后从 DOM 元素中获取组件类实例。早期的 Angular 类中有一个方法,但仅用于测试目的,而不是用于生产,我不确定 Angular.dart 1.0 是否完全支持该方法,但我认为这是在最近的版本之一中添加的。这样做通常表明您以错误的方式使用 Angular。

标签: dart angular-dart


【解决方案1】:

我认为ngProbe 可以做到这一点。

ElementProbe ep = ngProbe('my-component');

您可以通过访问https://pub.dartlang.org/packages/angular 的链接找到示例,其中提到了ngProbeElementProbe

CHANGELOG.md 提到 ngProbe 是动画所必需的,但 ElementProbe 类上的 codedoc 仍然声明它用于测试和调试(请参阅 https://github.com/angular/angular.dart/blob/96c0bcc7b6b0c3501b2c4799f425de8cd2e4dc0c/lib/core_dom/view_factory.dart#L259

要获取 JSON,您要么必须实现返回所需 JSON 的 getter/方法(例如 toJson),要么使用一些可用的序列化解决方案(请参阅 State of Serialization and Deserialization of JSON in Dart)。

如果你想这样做,这通常表明你的方法不符合 Angular 的使用方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    相关资源
    最近更新 更多