扩展NativeScript控件-SetNativeView

时间:2019-02-05 22:24:45

标签: angular typescript nativescript

我想扩展NativeScript Switch UI组件并将其用作自定义指令。

@Directive({
    selector: "CustomSwitch"
})
export class CustomSwitch extends Switch {
    constructor() {
        super();
        // Add custom events here
        if (isIOS) {
            // Change background and other properties here.
        }
    }
}

我可以看到ViewBase类像setNativeView这样的方法。任何人都可以分享一个示例,说明如何实现此目标以及实现此目标的更好方法。

CSS可以帮助进行全局样式设置,但我还需要具有扩展组件以公开自定义事件的功能。

1 个答案:

答案 0 :(得分:0)

我认为您正在混合使用NativeScript组件和Angular功能,如果需要指令,则不必扩展Switch或任何{N}组件。

只需应用选择器并将ElementRef注入到指向Switch(或应用了选择器的元素)的构造函数中,您只需更改其背景颜色即可。就像您在Angular Web应用程序中所做的一样。

@Directive({
  selector: "CustomSwitch"
})
export class CustomSwitchDirective implements AfterViewInit {
  constructor(private el: ElementRef) { }

  ngAfterViewInit() {
    const switch = <Switch>this.el.nativeElement;
    switch.backgroundColor = "red";
  }
}
相关问题