【问题标题】:Is there a possibility to change the color of the button?是否有可能更改按钮的颜色?
【发布时间】:2018-03-19 17:33:50
【问题描述】:

我希望当用户将他的用户名和密码写入输入时按钮的颜色会发生变化,以便可以知道按钮的标志。 (所以灰色的变化 -> 绿色)

CSS

   .turn-green {
    background-color: green;
}

TS

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  userWroteName: false;

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    function userTyped(value) { // fires on each keystroke
    const minLengthForName = 3; // you choose your min accepted length
    // some code you want here
    if (value.length >= minLengthForName) {
        this.userWroteName = true;
    } else { 
        this.userWroteName = false; 
    }
}


  }



  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }


}

【问题讨论】:

  • 请查看How do I ask a good question? 按钮? - 哪个按钮?将代码添加到您的问题中。你都尝试了些什么?是 angularjs (v1) 还是使用 typescript 和 Angular2+ 的 ionic2,你的标签没有意​​义。
  • 问题很明显。他需要在某些情况下使用 ngClass 更改按钮的颜色。人们可能会花费数小时寻找如何做这样简单的事情,而像“你做得不够”这样的 cmets 并没有帮助。我们都去过那里。这不像他问如何初始化一个 int 什么的。

标签: angular typescript sass ionic2


【解决方案1】:

<button> 元素上的*.html 文件中,使用[ngClass]

<button [ngClass]="{'turn-green': userWroteName}">My Button</button>

在您的 *.css 文件中定义 turn-green

.turn-green {
    background-color: green;
}

在您的*.ts 文件中创建布尔变量userWroteName,然后根据用户是否在文本框中写入内容来触发一些函数。

userWroteName: false;

function userTyped(value) { // fires on each keystroke
    const minLengthForName = 3; // you choose your min accepted length
    // some code you want here
    if (value.length >= minLengthForName) {
        this.userWroteName = true;
    } else { 
        this.userWroteName = false; 
    }
}

此代码假定您正在监听每次击键,并在每次键入时调用 userTyped()

【讨论】:

  • 所以我写 userWroteName: false;在导出类中,其余在构造函数中?因为现在什么都没有发生。如果我这样做。
  • 它不进入构造函数。它是 *.ts 文件中的一个单独函数。您的输入字段上有监听器吗?
  • 不,对不起,我不知道那是什么:(但我可以很快用谷歌搜索。
  • 您可能在 html 中的某处定义了一个 ,它会创建您的文本字段。在此处添加一些内容: 这是您击键的侦听器,它将调用函数 userTyped 并将当前值传递给它盒子。
  • 好的,我已经做到了,谢谢。现在还有最后一个问题:*.ts 文件的变量和函数?那么是哪一个:components.ts、module.ts、main.ts、login.ts、login.module.ts?很抱歉有很多问题,但我对 Ionic 很陌生
猜你喜欢
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 2014-07-11
相关资源
最近更新 更多