How to communicate between Angular DART controllers

前端 未结 3 1467
旧巷少年郎
旧巷少年郎 2020-12-04 02:49

i have two controllers and want to \"send\" between them object. I have something like this:

@NgController(selecto         


        
相关标签:
3条回答
  • 2020-12-04 03:04

    You could use
    * scope.$emit
    * scope.$broadcast
    * scope.$on

    @grohjy s solution might work also, depending on your requirements

    Scope scope;
    UserController(this.scope) { // get access to the scope by adding it to the constructor parameter list
      // sender
      scope.$emit('my-event-name', [someData, someOtherData]); // propagate towards root
      scope.$broadcast('my-event-name', [someData, someOtherData]); // propagate towards leaf nodes (children)
      scope.$parent.$broadcast('my-event-name', [someData, someOtherData]); // send to parents childs (includes silblings children)
      scope.$root.$broadcast('my-event-name', [someData, someOtherData]); // propagate towards leaf nodes starting from root (all nodes)
    
      // receiver
      scope.$on('my-event-name', (ScopeEvent e) => myCallback(e)); // call myCallback when an `my-event-name` event reaches me
    }
    

    just write scope.$emit (or one of the other methods) and ctrl+mouseclick to navigate to the the doc comments to get more information.

    0 讨论(0)
  • 2020-12-04 03:07

    I don't fully follow your question, could you include the whole code for better understanding.

    Here is one example, which might answer to your question: https://github.com/angular/angular.dart/issues/264

    0 讨论(0)
  • 2020-12-04 03:10

    With the newest AngularDart library (0.10.0), Günter Zöchbauer's solution is still correct, but the syntax has changed a bit:

    // Receiver
    //import 'dart:async';
    String name;
    Scope scope;
    ReceiverConstructor(this.scope) {
      Stream mystream = scope.on('username-change');
      mystream.listen(myCallback);
    }
    
    void myCallback(ScopeEvent e) {
      this.name = e.data;
    }
    
    
    // Sender
    scope.emit("username-change", "emit");
    scope.broadcast("username-change", "broadcast");
    scope.parentScope.broadcast("username-change", "parent-broadcast");
    scope.rootScope.broadcast("username-change", "root-broadcast");
    
    0 讨论(0)
提交回复
热议问题