Apply default style and onClick change style of a button -Angular 4(应用默认样式和 onClick 更改按钮的样式-Angular 4)
问题描述
我有一个按钮,我想应用按钮的默认样式,当用户单击按钮时,将按钮样式颜色更改为红色,将背景颜色更改为白色.Blow 是我的 .css 和组件.
.btn-default {白颜色;背景颜色:蓝色;}.btn 更改 {红色;背景颜色:白色;}
组件.ts
从'@angular/core'导入{组件};@零件({选择器:'应用程序根',templateUrl: './app.component.html',styleUrls: ['./app.component.css']})导出类 AppComponent {bntStyle:字符串;应用组件() {这.bntStyle = 'btn-default';}提交() {this.bntStyle = 'btn-change';}
.html
<button name="保存" [ngClass]="['bntStyle']" (onClick)="submit()">提交</button></div> 解决方案 你正在绑定字符串'btnStyle'.相反,您应该绑定归档:
<button name="保存" [ngClass]="[bntStyle]" (click)="submit()">提交</button></div>I have one button, i want to apply a default style of a button and when user click on a button change a button style color to red and background-color to white. Blow is my .css and component.
.btn-default {
color: white;
background-color: blue;
}
.btn-change {
color: Red;
background-color: white;
}
Component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
bntStyle: string;
AppComponent() {
this. bntStyle = 'btn-default';
}
submit() {
this.bntStyle = 'btn-change';
}
.html
<div>
<button name="Save" [ngClass]="['bntStyle']" (onClick)="submit()">Submit</button>
</div>
解决方案 You are binding the string 'btnStyle'. Instead, you should bind the filed:
<div>
<button name="Save" [ngClass]="[bntStyle]" (click)="submit()">Submit</button>
</div>
这篇关于应用默认样式和 onClick 更改按钮的样式-Angular 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:应用默认样式和 onClick 更改按钮的样式-Angular 4


基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01