如何在 angular primeng 中设置 p-listbox 的宽度?

how to set width for p-listbox in angular primeng?

我正在尝试将 p-listbox 组件宽度设置为 10 REM,但是我的更改无法实现。请在这里建议。我正在尝试在 css class 上应用宽度。但是,它不适用。但是,当我在 chrome 开发工具中使用 class .ui-listbox-list-wrapper 应用 div 时,它适用得很好。

dashboard.component.ts

import { Component, OnInit } from '@angular/core';  
import { SocialLoginModule, AuthServiceConfig, AuthService } from 'angular-6-social-login';  
import { Router } from '@angular/router';  
import { SocialUsers } from '../../model/social-users';
class City {
  name:string;
  code:string;
}


@Component({  
  selector: 'app-dashboard',  
  templateUrl: './dashboard.component.html',  
  styleUrls: ['./dashboard.component.css']  
})  


export class DashboardComponent implements OnInit {  
  socialusers = new SocialUsers();  

  cities1: City[];
  selectedCity1:string;



  constructor(public OAuth: AuthService,    private router: Router) { 

    this.cities1 = [
      {name: 'New York', code: 'NY'},
      {name: 'Rome', code: 'RM'},
      {name: 'London', code: 'LDN'},
      {name: 'Istanbul', code: 'IST'},
      {name: 'Paris', code: 'PRS'}
  ];
  }  
  ngOnInit() {  
    this.socialusers = JSON.parse(localStorage.getItem('socialusers'));  
    console.log(this.socialusers);  
  }  
  logout() {  
    this.OAuth.signOut().then(data => {  
      debugger;  
      this.router.navigate(['login']);  
    });  
  }  
}

dashboard.component.html

<p-listbox id="listId" [options] ="cities1" [(ngModel)] ="selectedCity1" optionLabel = "name">

</p-listbox>

selected city={{selectedCity1?.name}}

dashboard.component.css

.ui-listbox-list-wrapper {
    width: 20REM;
}

您不能直接从另一个组件更改组件的样式。你需要使用 ::ng-deep

dashboard.component.css

:host ::ng-deep p-listbox .ui-listbox-list{
    width: 20rem;
}

另一种选择:primeng 提供了一种通过提供自定义 class

来更改任何组件样式的方法
<p-listbox styleClass="listbox-20rem" [options]="cities1" [(ngModel)] ="selectedCity1" optionLabel="name">
</p-listbox>

在全局样式文件中

style.css

.listbox-20rem .ui-listbox-list {
  width: 20rem;
}

检查这个demo