angular - 如何在 Dart Angular 中编写双向绑定(bind)

标签 angular dart

我需要一个组件来选择具有双向绑定(bind)的用户 Angular 色

role-chooser-comp.html

<div class="roleChooser">
  <role-item #owner (select)="role(owner.role)" [role]="'owner'" [title]="'Owner'"></role-item>
  <role-item #writer (select)="role(writer.role)" [role]="'writer'" [title]="'Writer'"></role-item>
  <role-item #viewer (select)="role(viewer.role)" [role]="'viewer'" [title]="'Reader'"></role-item>
</div>

它的类如下:

role-chooser-comp.dart

@Component(
    selector: 'role-chooser-comp',
    templateUrl: 'role_chooser_comp.html',
    styleUrls: const ['role_chooser_comp.css'],
    directives: const [RoleItem])
class RoleChooser implements OnInit {
  final PlaceService _placeService;
  final Router _router;
  final Environment _environment;

  @ViewChild('owner') RoleItem owner;
  @ViewChild('writer') RoleItem writer;
  @ViewChild('viewer') RoleItem viewer;
  List<RoleItem> choices;
  String lastSelected;

  RoleChooser(this._placeService, this._router, this._environment) {

  }

  Future<Null> ngOnInit() async {
    choices = [owner, writer, viewer];
    if (lastSelected != null)
      role(lastSelected);
  }

  String get selected => lastSelected;

  @Input()
  set selected(String role) {
    //on init, the choices are still not set
    if (choices == null)
      lastSelected = role;
    else
      this.role(role);
  }

  void role(String role) {
    if (choices == null) {
      window.alert("No roles are set");
      return;
    }

    for (RoleItem item in choices) {
      if (item.role == role) {
        item.selected = true;
        lastSelected = role;
      } else {
        item.selected = false;
      }
    }
  }
}

现在我需要的是能够在如下用途中自动更新模型的值:

usecase

<role-chooser-comp [(selected)]="userRole.roleName"></role-chooser-comp>

<role-chooser-comp [(ngModel)]="userRole.roleName"></role-chooser-comp>

我找到了一篇关于 angular.js 的文章,它展示了如何将 [(ngModel)] 与任何元素一起使用(实现两种方法,以便 angular 知道如何从组件和向后获取值):https://docs.angularjs.org/guide/forms (部分实现自定义表单控件(使用 ngModel))

我想在 Dart 中做同样的事情,但我不知道如何...

Angular 色项暂时很简单,我贴出来供引用:

role-item.html

<div class="role" [class.selected]="selected" (click)="clicked()">
      <btn #icon
          [sources]="images"></btn>
    </div>

role-item.dart

import 'package:angular2/core.dart';
import 'package:angular2/router.dart';

import 'package:share_place/environment.dart';
import 'package:share_place/place_service.dart';

import 'package:share_place/common/ui/button_comp.dart';

@Component(
    selector: 'role-item',
    templateUrl: 'role_item.html',
    styleUrls: const ['role_item.css'],
    directives: const [ButtonComp])
class RoleItem {
  final PlaceService _placeService;
  final Router _router;
  final Environment _environment;
  @Output() final EventEmitter<String> pressAction = new EventEmitter<String>();
  @Output() final EventEmitter<String> select = new EventEmitter<String>();

  get role => itemRole;

  @Input() set role(String role) {
    itemRole = role;
    images = [
      '../images/roles/$role.png',
      '../images/roles/$role-h.png',
      '../images/roles/$role-c.png'
    ];
  }

  @Input() String title;
  @Input() String desc;

  @ViewChild('icon')
  ButtonComp icon;

  String itemRole;

  List<String> images;
  bool isSelected = false;

  bool toggle = false;

  RoleItem(this._placeService, this._router, this._environment);

  void clicked() {
    bool newlySelected;
    if (!isSelected)
      newlySelected = true;

    if (toggle)
      selected = !selected;
    else
      selected = true;

    pressAction.emit(role);
    if (newlySelected)
      select.emit(role);
  }

  bool get selected => isSelected;

  void set selected(bool isSelected) {
    this.isSelected = isSelected;
    icon.select(isSelected);
  }

}

最佳答案

你需要添加

@Output() EventEmitter<String> selectedChange = new EventEmitter<String>();

到您的 RoleChooser 组件。

要通知更改调用

selectedChange.add('foo');

然后 userRole.roleName 来自

<role-chooser-comp [(selected)]="userRole.roleName"></role-chooser-comp>

将会更新。

关于angular - 如何在 Dart Angular 中编写双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42716811/

相关文章:

javascript - 如何使 mat-sort-header 不能全部点击

flutter - 错误 : Cannot run with sound null safety, 因为以下依赖项(flutter_absolute_path)

javascript - Angular - 如何使用 InnerHtml 执行存储在字符串中的脚本

angular - ngrx/data 实体数据服务

flutter - 如何删除文本框的标签?

Flutter:用 child 的 GestureDetector 覆盖 'PageView' 滚动手势

mobile - 如何在 Flutter 中使页面背景半透明以显示上一屏幕?

dart - 布局更改不适用于测试

angular - 在嵌套 react 形式中使用 setControl

javascript - 防止 Router.Navigate 上的页面跳转