typescript SD-7.ts

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript SD-7.ts相关的知识,希望对你有一定的参考价值。

export const VALUE_ACCESSOR = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => NbDropdownComponent),
  multi: true,
};

@Component({
  selector: 'nb-dropdown',
  template: `
    <div class="dropdown" [class.open]="isOpen" (click)="isOpen = !isOpen">
     <button>
       <ng-container [ngTemplateOutlet]="match.tpl"    [ngOutletContext]="selected">
       </ng-container>
       <span *ngIf="!selected.$implicit">{{placeholder}}</span>       
     </button>
     <ul> 
        <li *ngFor="let option of nbSelectOptions" (click)="select(option)">
          <a href="#">
           <ng-container [ngTemplateOutlet]="option.tpl">
           </ng-container>
          </a>
        </li>
     </ul>
    </div>
  `,
  providers: [VALUE_ACCESSOR]
})
export class NbDropdownComponent implements ControlValueAccessor {
  isOpen = false;
  @Input() placeholder;
  @ContentChild(NbSelectMatchDirective) match;
  @ContentChildren(NbSelectOptionDirective) nbSelectOptions;
  selected = {}

  writeValue(selected) : void {
    if( selected ) {
      this.setSelected(selected);
    }
  }

  registerOnChange(fn) : void {
    this.onChange = fn;
  }

  select(nbSelectMatch) {
    this.setSelected(nbSelectMatch);
    this.onChange(nbSelectMatch.option);
  }

  setSelected(nbSelectMatch) {
    this.selected = {
      $implicit: nbSelectMatch.option ? 
                 nbSelectMatch.option : 
                 nbSelectMatch
    }
  }
}

以上是关于typescript SD-7.ts的主要内容,如果未能解决你的问题,请参考以下文章

typescript TypeScript Snippets #typescript

TypeScript入门五:TypeScript的接口

TypeScript系列教程--初探TypeScript

TypeScript入门三:TypeScript函数类型

typescript使用 TypeScript 开发 Vue 组件

认识 TypeScript