Angular Dart component events

旧街凉风 提交于 2019-11-29 11:52:57

This should be the same, just add a scope argument to the constructor so the component gets the scope injected.

There was a related change in Angular 0.14.0 https://github.com/angular/angular.dart/commit/181f01448555c475869505491159045904e5dc89

I haven't yet tried this. From the description you need to implement 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) => ...);
  }
}

Based on the answer from Günter i built this working example:

@Component(
    selector: "confirm-component",
    templateUrl: 'component/confirm.html',
    useShadowDom: false,
    publishAs: "ctrl"
)

class ConfirmComponent implements ScopeAware {
    Scope scope;

    void yes(){
        scope.emit('confirm', 'yes');
    }

    void no(){
        scope.emit('confirm', 'no');
    }
}

@Component(
    selector: "my-component",
    templateUrl: 'component/my.html',
    useShadowDom: false,
    publishAs: "ctrl"
)
class MyComponent implements ScopeAware{

    void set scope(Scope scope) {
        Stream mystream = scope.on('confirm');
        mystream.listen((event){
            print('confirmed: ' + event.data);
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!