【问题标题】:Button color adjustment by data(Ionic2)通过数据调整按钮颜色(Ionic2)
【发布时间】:2017-11-12 14:28:29
【问题描述】:

我有一个 json 数据库。我希望不同的 Id 具有不同的颜色。如果 Id = 1,则将按钮设为蓝色。如果 Id = 0,则将按钮设为红色。您知道我该怎么做吗?

home.html

      <ion-header>
       <ion-navbar>
         <ion-title>
           Ionic Blank
         </ion-title>
       </ion-navbar>
     </ion-header>


     <ion-content padding>
         <ion-list >
           <ion-item *ngFor="let item of getdata">
              {{item.name}} {{item.id}}
         <button ion-button round class="ilanbtnn" >{{item.surname}</button> 
            </ion-item>
            </ion-list>
     </ion-content>

home.ts

     import { Component } from '@angular/core';
     import { NavController } from 'ionic-angular';
     import {Http,Response} from '@angular/http';
     import {DataService} from '../../app/dataapi/data-api.service';

    @Component({
    selector: 'page-home',
     templateUrl: 'home.html'
     })
     export class HomePage {

      getdata : any;


      constructor(public navCtrl: NavController ,public http:Http,public 
     dataApi:DataService) {

    }

     ionViewDidLoad() { 

       this.dataApi.getVideo().then(data =>
        this.getdata = data);   
    }

 }

【问题讨论】:

    标签: css angular typescript ionic2 ionic3


    【解决方案1】:

    可以通过属性绑定来根据id设置颜色:

     <ion-content padding>
             <ion-list >
               <ion-item *ngFor="let item of getdata">
                  {{item.name}} {{item.id}}
             <button [color]="item.id === 1 ? 'custom-blue' : 'custom-red'" ion-button round class="ilanbtnn" >{{item.surname}</button> 
                </ion-item>
                </ion-list>
         </ion-content>
    

    请注意custom-bluecustom-red 必须是variables.scss 文件中colors 数组的一部分:

    $colors: (
      primary:    #387ef5,
      secondary:  #32db64,
      danger:     #f53d3d,
      light:      #f4f4f4,
      dark:       #222,
    
      //...
    
      custom-blue:    #387ef5,
      custom-red:     #f53d3d
    );
    

    【讨论】:

    • 很高兴为您提供帮助:)
    • 我不建议现在向 $colors 数组添加太多,因为 ionic 中的每个组件都会在数组中的每种颜色中生成。对于本机应用程序可能无关紧要,但它会导致 web 应用程序的 main.css 相当大。
    • 感谢@rory_za 指出这一点(我根本不知道)。就像您说的那样,我们可以添加到应用程序中的每一项性能改进都可能对最终用户产生影响。再次感谢您的评论:)
    • 这里是 github 问题,如果您想订阅并在任何更改时收到通知:github.com/ionic-team/ionic-app-scripts/issues/506
    猜你喜欢
    • 1970-01-01
    • 2023-02-13
    • 2015-03-21
    • 2016-09-11
    • 2021-12-16
    • 2017-10-26
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多