Javascript 角形4型母管和路由器插座
假设我们有以下代码:Javascript 角形4型母管和路由器插座,javascript,angular,twitter-bootstrap-3,Javascript,Angular,Twitter Bootstrap 3,假设我们有以下代码: 您可以通过以下方式完成此项工作: [style.background-color]="getBackgroundColor()" 在component.ts中: constructor(private router: Router) { } ngOnInit() { this.router.events.subscribe((route: any) => this.route= route); } getBackgroundColor() { i
您可以通过以下方式完成此项工作:
[style.background-color]="getBackgroundColor()"
在component.ts中:
constructor(private router: Router) { }
ngOnInit() {
this.router.events.subscribe((route: any) => this.route= route);
}
getBackgroundColor() {
if (this.url) {
if (this.route.url === '/yourRoute'{
return 'white'
} else {
return '#6772e5'
}
} else {
return '#6772e5'
}
}
有几种方法可以做到这一点。如果希望父组件在某个子组件处于活动状态时更改样式,则需要进行一些编码才能完成 方法1:共享服务 您需要创建一个共享服务来控制父组件订阅的状态。方法“emitChange”将更改状态“fillColorSubject”,该状态作为可观察的“fillColor”返回以订阅 填充颜色服务。ts:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FillColorService {
private fillColorSubject = new Subject<boolean>();
fillColor = this.fillColorSubject.asObservable();
emitChange(fill : boolean) {
this.fillColorSubject.next(fill);
}
}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
constructor ( private _fillColorService: FillColorService ) {
}
ngOnInit() {
this._fillColorService.emitChange(true);
}
ngOnDestroy() {
this._fillColorService.emitChange(false);
}
}
import { Component } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
constructor ( private _fillColorService: FillColorService ) {
_fillColorService.fillColor .subscribe(
fill => {
this.fillColumn = fill;
});
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component></app-component>
</div>
</div>
</div>
import { Component } from '@angular/core';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
fillColor( fill : boolean = false ) {
this.fillColumn = fill;
}
}
import { Component, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
@Component( {
selector: 'app-component',
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
@Output() fillParentColor = new EventEmitter<boolean>();
ngOnInit() {
this.fillParentColor.emit(true);
}
ngOnDestroy() {
this.fillParentColor.emit(false);
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component (fillParentColor)="fillColor($event)"></app-component>
</div>
</div>
</div>
现在,父组件将需要订阅该服务。当状态更改时,开关(fillColumn)将更改列状态
父.component.ts:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FillColorService {
private fillColorSubject = new Subject<boolean>();
fillColor = this.fillColorSubject.asObservable();
emitChange(fill : boolean) {
this.fillColorSubject.next(fill);
}
}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
constructor ( private _fillColorService: FillColorService ) {
}
ngOnInit() {
this._fillColorService.emitChange(true);
}
ngOnDestroy() {
this._fillColorService.emitChange(false);
}
}
import { Component } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
constructor ( private _fillColorService: FillColorService ) {
_fillColorService.fillColor .subscribe(
fill => {
this.fillColumn = fill;
});
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component></app-component>
</div>
</div>
</div>
import { Component } from '@angular/core';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
fillColor( fill : boolean = false ) {
this.fillColumn = fill;
}
}
import { Component, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
@Component( {
selector: 'app-component',
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
@Output() fillParentColor = new EventEmitter<boolean>();
ngOnInit() {
this.fillParentColor.emit(true);
}
ngOnDestroy() {
this.fillParentColor.emit(false);
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component (fillParentColor)="fillColor($event)"></app-component>
</div>
</div>
</div>
您的模板需要能够通过条件类反映更改
parent.component.html:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FillColorService {
private fillColorSubject = new Subject<boolean>();
fillColor = this.fillColorSubject.asObservable();
emitChange(fill : boolean) {
this.fillColorSubject.next(fill);
}
}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
constructor ( private _fillColorService: FillColorService ) {
}
ngOnInit() {
this._fillColorService.emitChange(true);
}
ngOnDestroy() {
this._fillColorService.emitChange(false);
}
}
import { Component } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
constructor ( private _fillColorService: FillColorService ) {
_fillColorService.fillColor .subscribe(
fill => {
this.fillColumn = fill;
});
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component></app-component>
</div>
</div>
</div>
import { Component } from '@angular/core';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
fillColor( fill : boolean = false ) {
this.fillColumn = fill;
}
}
import { Component, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
@Component( {
selector: 'app-component',
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
@Output() fillParentColor = new EventEmitter<boolean>();
ngOnInit() {
this.fillParentColor.emit(true);
}
ngOnDestroy() {
this.fillParentColor.emit(false);
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component (fillParentColor)="fillColor($event)"></app-component>
</div>
</div>
</div>
然后,子myComponent需要发出一个带有布尔值的事件(fillParentColor),以触发父更改
my.component.ts:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FillColorService {
private fillColorSubject = new Subject<boolean>();
fillColor = this.fillColorSubject.asObservable();
emitChange(fill : boolean) {
this.fillColorSubject.next(fill);
}
}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
constructor ( private _fillColorService: FillColorService ) {
}
ngOnInit() {
this._fillColorService.emitChange(true);
}
ngOnDestroy() {
this._fillColorService.emitChange(false);
}
}
import { Component } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
constructor ( private _fillColorService: FillColorService ) {
_fillColorService.fillColor .subscribe(
fill => {
this.fillColumn = fill;
});
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component></app-component>
</div>
</div>
</div>
import { Component } from '@angular/core';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
fillColor( fill : boolean = false ) {
this.fillColumn = fill;
}
}
import { Component, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
@Component( {
selector: 'app-component',
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
@Output() fillParentColor = new EventEmitter<boolean>();
ngOnInit() {
this.fillParentColor.emit(true);
}
ngOnDestroy() {
this.fillParentColor.emit(false);
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component (fillParentColor)="fillColor($event)"></app-component>
</div>
</div>
</div>
import{Component,Output,EventEmitter,OnInit,ondestory}来自'@angular/core';
@组件({
选择器:“应用程序组件”,
templateUrl:'my.component.html'
} )
导出类myComponent实现OnInit、OnDestroy{
@Output()fillParentColor=neweventemitter();
恩戈尼尼特(){
this.fillParentColor.emit(true);
}
恩贡德斯特罗(){
this.fillParentColor.emit(false);
}
}
最后,您的模板需要能够在发出子事件时触发父组件事件。触发myComponent的“fillParentColor”事件时,将使用布尔参数触发parentComponent的“fillColor”事件。您还需要根据“fillColumn”开关为列添加条件类(fill)
parent.component.html:
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class FillColorService {
private fillColorSubject = new Subject<boolean>();
fillColor = this.fillColorSubject.asObservable();
emitChange(fill : boolean) {
this.fillColorSubject.next(fill);
}
}
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
constructor ( private _fillColorService: FillColorService ) {
}
ngOnInit() {
this._fillColorService.emitChange(true);
}
ngOnDestroy() {
this._fillColorService.emitChange(false);
}
}
import { Component } from '@angular/core';
import { FillColorService } from 'fill-color-service';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
constructor ( private _fillColorService: FillColorService ) {
_fillColorService.fillColor .subscribe(
fill => {
this.fillColumn = fill;
});
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component></app-component>
</div>
</div>
</div>
import { Component } from '@angular/core';
@Component( {
templateUrl: 'parent.component.html'
} )
export class parentComponent {
fillColumn : boolean;
fillColor( fill : boolean = false ) {
this.fillColumn = fill;
}
}
import { Component, Output, EventEmitter, OnInit, OnDestroy } from '@angular/core';
@Component( {
selector: 'app-component',
templateUrl: 'my.component.html'
} )
export class myComponent implements OnInit, OnDestroy {
@Output() fillParentColor = new EventEmitter<boolean>();
ngOnInit() {
this.fillParentColor.emit(true);
}
ngOnDestroy() {
this.fillParentColor.emit(false);
}
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<app-sidebar></app-sidebar>
</div>
<div class="col-sm-8 col-md-9" [ngClass]="{ 'fill': fillColumn }">
<app-component (fillParentColor)="fillColor($event)"></app-component>
</div>
</div>
</div>
谢谢你,旺德里尔