角度ngbootstrap单选按钮颜色不会动态更改

时间:2019-01-14 08:44:39

标签: css angular ng-bootstrap

我正在尝试更改按钮按下或单击事件时按钮的颜色。即使是边框色的背景色也可以。但是此代码段未应用所需的颜色。似乎方法被调用了,但颜色没有应用。

  

buttons-radioreactive.html

<form [formGroup]="radioGroupForm">
      <div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" formControlName="model">
        <label ngbButtonLabel class="btn-primary">
          <input ngbButton type="radio" [value]="1" > Left (pre-checked)
        </label>
        <label #value1 ngbButtonLabel class="btn-primary" (click)="call()">
          <input ngbButton type="radio" value="middle"> Middle
        </label>
        <label ngbButtonLabel class="btn-primary">
          <input ngbButton type="radio" [value]="false"> Right
        </label>
      </div>
    </form>
    <hr>
    <pre>{{radioGroupForm.value['model']}}</pre>
  

buttons-radioreactive.ts

    import { Component, OnInit } from '@angular/core';
    import { FormBuilder, FormGroup } from '@angular/forms';
    import {ElementRef} from '@angular/core';
    import { ViewChild } from '@angular/core/';


    @Component({
      selector: 'ngbd-buttons-radioreactive',
      templateUrl: './buttons-radioreactive.html',
      styleUrls: ['./buttons-radioreactive.css']
    })
    export class NgbdButtonsRadioreactive implements OnInit {
    @ViewChild('value1')el:ElementRef;

      public radioGroupForm: FormGroup;

      constructor(private formBuilder: FormBuilder) {}

      ngOnInit() {
        this.radioGroupForm = this.formBuilder.group({
          'model': 1
        });
      }

      call(){
        console.log("Called before")
        this.el.nativeElement.color="orange";
        console.log("Called after")
      }
    }
  

buttons-radioreactive.css

    .pressed {
  border-color: #ff9800;
  color: orange;
}

.un-pressed {
  border-color: #ffffff;
  color: white;
}

3 个答案:

答案 0 :(得分:0)

请检查this

在您的HTML中,您仅从“中间”单选按钮调用call()方法...因此,您想要的效果仅应用于它。

import {  Component,  OnInit,  Renderer2 ,ElementRef} from '@angular/core';
import {  FormBuilder,  FormGroup} from '@angular/forms';
import {  ViewChild} from '@angular/core/';

@Component({
  selector: 'ngbd-buttons-radioreactive',
  templateUrl: './buttons-radioreactive.html',
  styles: [`
          .pressed {
  border-color: #ff9800;
  color: orange;
}

.un-pressed {
  border-color: #ffffff;
  color: white;
}
`]
})
export class NgbdButtonsRadioreactive implements OnInit {
  @ViewChild('value1') el: ElementRef;

  public radioGroupForm: FormGroup;

  constructor(private formBuilder: FormBuilder, private renderer: Renderer2) {}

  ngOnInit() {
    this.radioGroupForm = this.formBuilder.group({
      'model': 1
    });
  }

  call() {
    this.renderer.addClass(this.el.nativeElement, 'pressed');
  }
}

答案 1 :(得分:0)

尝试使用ngClass:

在组件中:

toggle : boolean = true;
call(){
  this.toggle = !this.toggle;
}

在html中:

<div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" [(ngModel)]="model">
    <label ngbButtonLabel class="btn-primary">
          <input ngbButton type="radio" [value]="1" > Left (pre-checked)
        </label>
        <label #value1 ngbButtonLabel class="btn-primary"
        [ngClass]="{'pressed': toggle , 'un-pressed': !toggle}">
          <input ngbButton type="radio" value="middle"  (click)="call()"> Middle // consider to add click to input
        </label>
        <label ngbButtonLabel class="btn-primary">
          <input ngbButton type="radio" [value]="false"> Right
        </label>
      </div>
    <hr>
<pre>{{test}}</pre>

css:

.pressed {
  border-color: #ff9800 !important; // important
  color: orange !important;
}

.un-pressed {
  border-color: #ffffff !important;
  color: white !important;
}

DEMO

答案 2 :(得分:0)

您可以为按钮分配自定义类别,请参见stackblitz

styles:[`
    .btn-custom.focus
    {
      outline:none!important;
      box-shadow:0 0 0 0
    }
    .btn-custom{
      background-color:orange;
      color:white;

    }
    .btn-custom.active{
      background-color:red;
      color:white;
      border-color:firebrick;
    }
  `
<div class="btn-group btn-group-toggle" ngbRadioGroup name="radioBasic" [(ngModel)]="model">
  <label ngbButtonLabel class="btn-custom">
    <input ngbButton type="radio" [value]="1"> Left (pre-checked)
  </label>
  <label ngbButtonLabel class="btn-custom">
    <input ngbButton type="radio" value="middle"> Middle
  </label>
  <label ngbButtonLabel class="btn-custom">
    <input ngbButton type="radio" [value]="false"> Right
  </label>
</div>
<hr>
<pre>{{model}}</pre>