Do publishAs values have to be unique in Angular D

2019-08-03 15:50发布

问题:

I suspect this is a bug, possibly even a manifestation of https://github.com/angular/angular.dart/issues/396.

I want to register multiple controllers and want to use ctrl as the value of the publishAt field within @NgDirective. This leads to the second ctrl clobbering the value of the first one, even though they exist in different scopes.

Here is the view:

<!DOCTYPE html>
<html>
  <body>    
    <div>
      <div foo-controller>

        <!-- PRINTS 'bar'. -->
        <p>{{ctrl.item}}</p>
      </div>

      <div bar-controller>
        <!-- PRINTS 'bar'. -->           
        <p>{{ctrl.item}}</p>
      </div>
    </div>

    <script type="application/dart" src="main.dart"></script>
    <script type="text/javascript" src="packages/browser/dart.js"></script>
  </body>
</html>

And here is the Dart code:

import 'package:angular/angular.dart';

@NgDirective(
  selector: '[foo-controller]',
  publishAs: 'ctrl'
)
class FooController {
  String item = 'foo';
}

@NgDirective(
  selector: '[bar-controller]',
  publishAs: 'ctrl'
)
class BarController {
  String item = 'bar';
}

main() {
  ngBootstrap(module: new Module()
      ..type(FooController)
      ..type(BarController));
}

The output is 'bar' both times. If I make the publishAs values unique, the output is 'foo' followed by 'bar'.

Is this a bug or am I misunderstanding how scopes work?

回答1:

Duplicate of: https://github.com/angular/angular.dart/issues/396 Puzzled by CSS selector to trigger controller



回答2:

You should use @NgController rather than @NgDirective