Angular 2绑定以形成选择值

时间:2016-05-03 16:46:28

标签: typescript angular

我正在尝试控制元素的颜色,等待用户选择表单选择元素。目前我只能静态地工作。

selectedEventGroup: string[]; //这会在表单选择下拉列表中抓取用户选择。

color: string = SchedulingMatrix.selectedEventGroup.value; //这不起作用返回错误。 “在SchedulingMatrix类型上不存在selectedEventGroup,它本身可能是另一个问题。

color: string = "green"; //这有效但是是静态的

我的选择组件<p-dropdown [options]="eventGroup" [(value)]="eventGroup" style="width: 180px;"></p-dropdown>

import....

@Component({
  ...
})
export class SchedulingMatrix implements OnInit {

  events: any[];

  event: MyEvent;

  dialogVisible: boolean = false;

  constructor(private eventService: EventService, private cd: ChangeDetectorRef) {

    this.eventGroup = [];
    this.eventGroup.push({
      label: 'Select Group',
      value: '#fff'
    });
    this.eventGroup.push({
      label: 'group 1',
      value: '#ccc'
    });
    this.eventGroup.push({
      label: 'group 2',
      value: '#ddd'
    });
    this.eventGroup.push({
      label: 'group 3',
      value: '#eee'
    });

  }

  eventGroup: SelectItem[];
  selectedEventGroup: string[]; //this grabs the users selection in the select dropdown.

}

export class MyEvent {
  id: number;
  title: string;
  start: string;
  end: string;
  color: string = "green"; //This works but is static
  //color: string = SchedulingMatrix.selectedEventGroup.value;  //Not sure what to use here to  hook up to the the users selection in the eventGroup
  allDay: boolean = true;
}

1 个答案:

答案 0 :(得分:0)

<select *ngIf="eventGroup" [(ngModel)]="selectedObject">
  <option *ngFor="let x of objectList" [ngValue]="x">
       {{...}}
  </option>
</select>

其中selectedObject是任何对象 并试试这个[ngValue]而不是[(value)] =“eventGroup”