Do publishAs values have to be unique in Angular D

2019-08-03 15:12发布

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?

2条回答
闹够了就滚
2楼-- · 2019-08-03 15:53

You should use @NgController rather than @NgDirective

查看更多
登录 后发表回答