events - Angular Dart 组件事件

标签 events scope dart components angular-dart

我正在尝试将自定义事件从组件传递到其父组件/ Controller

确认.html

<div class="comfirm-component">
    <content></content>
    <a href="#" ng-click="ctrl.yes()">Yes</a>
    <a href="#" ng-click="ctrl.no()">No</a>
</div>

确认.dart

@Component(
    selector: "confirm-component",
    templateUrl: 'confirm.html',
    useShadowDom: false,
    publishAs: "ctrl"
)
class ConfirmComponent {
    void yes(){
        print('yes');
        // Fire confirm-yes event
    }

    void no(){
        print('no');
        // Fire confirm-no event
    }
}

有这样的事情吗?:

<confirm-component on-confirm-yes="doSomething()" on-confirm-no="doSomethingElse()">
    Do you want to delete
</confirm-component>

我可以使用普通的 StreamController,但随后我必须用代码连接我的组件。

confirmComponent.onConfirmYes.listen()
confirmComponent.onConfirmNo.listen()

我还发现了这个: How to communicate between Angular DART controllers

还有这个: angulardart components - dispatch custom event

在这两个步骤中都提到了scope.emit。但我没有找到一种方法来将它与组件而不是 Controller 一起使用。有 angular.dart v0.14.0 的完整示例吗?

scope.emit 是我正在寻找的东西吗?

最佳答案

这应该是相同的,只需向构造函数添加一个作用域参数,以便组件获得注入(inject)的作用域。

Angular 0.14.0 https://github.com/angular/angular.dart/commit/181f01448555c475869505491159045904e5dc89 中有相关更改

我还没有尝试过这个。 根据描述,您需要实现 ScopeAware

@Component(...)
class MyComponent implements ScopeAware {
  Watch watch;
  MyComponent(Dependency myDep) {
    // It is an error to add a Scope / RootScope argument to the ctor and will result in a DI
    // circular dependency error - the scope is never accessible in the class constructor
  }

  void set scope(Scope scope) {
     // with this scope you should be able to use emit
     // This setter gets called to initialize the scope
     watch = scope.rootScope.watch("expression", (v, p) => ...);
  }
}

关于events - Angular Dart 组件事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175120/

相关文章:

javascript - $.proxy() 和 bind() 有什么区别?

在状态栏顶部 flutter 奇怪的填充

flutter - GitLab Flutter CI 失败并显示 "tojunit: command not found"

Java Swing : keeping the event handling maintanable

javascript - JS - 无法在自定义事件的处理程序中将其指向调用者

JavaScript:将函数本地范围引用为对象

java - 临时变量的范围——Eclipse/编译器是否优化?

dart - 如何使用 flutter 创建自定义弹出菜单

c# - 域实体上的事件?

c# - Microsoft Outlook 自动化 : Capturing a Save event