我正在尝试将自定义事件从组件传递到其父组件/ 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/