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

标签 css angular primeng

我正在尝试将 p-listbox 组件宽度设置为 10 REM,但是我的更改无法实现。请在这里建议。我正在尝试在 css 类上应用宽度。但是,它不适用。但是,当我在 chrome 开发工具中使用 .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;
}

enter image description here

最佳答案

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

dashboard.component.css

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

另一种选择:primeng 提供了一种通过提供自定义类来更改任何组件样式的方法 🎉

<p-listbox styleClass="listbox-20rem" [options]="cities1" [(ngModel)] ="selectedCity1" optionLabel="name">
</p-listbox>

在全局样式文件中

样式.css

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

检查这个 👉 demo 🚀🚀

关于css - 如何在 Angular primeng中设置p-listbox的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59437004/

相关文章:

node.js - 使用 Fastify 和 Nestjs 上传文件错误

javascript - 如何验证表单中的 Angular 响应式(Reactive)下拉列表

Angular/YouTube API - 拒绝显示 : in a frame because it set 'X-Frame-Options' to 'sameorigin

css - gulp concat JS 库 CSS

javascript - 根据选中的单选/复选框显示 div,多个实例

angular - 在哪里可以找到特定版本的文档

angular - 样式不适用于组件级别但适用于全局级别

angular - Prime-NG P-CHIPS 数字仅用于数字数组

css - Bootstrap 3 Glyphicon 错误

javascript - 在非事件窗口后 catch CSS 动画