Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

scope - Angular Dart component events

I am trying to pass custom events from a component to its parent component/controller

confirm.html

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

confirm.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
    }
}

is there something like this?:

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

I could use a normal StreamController but then i'd had to connect my components with code.

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

I also found this: How to communicate between Angular DART controllers

And this: angulardart components - dispatch custom event

In both treads scope.emit is mentioned. But i didn't find a way to use it with a component instead of a controller. Is there a full example vor angular.dart v0.14.0?

Is scope.emit the thing i'm searching for?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) => ...);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...