Q1。假设我想要改变用户在按下主“删除”按钮之前标记为删除的每个“项目”的外观。(这种即时的视觉反馈应该消除了“你确定吗?”对话框的需要。)用户将选中复选框以指示应该删除哪些项。如果复选框未选中,则该项应恢复其正常外观。
应用或删除CSS样式的最佳方法是什么?
Q2。假设我想允许每个用户个性化我的站点的呈现方式。例如,从固定的字体大小中选择,允许用户自定义前景色和背景颜色等。
应用用户选择/输入的CSS样式的最佳方法是什么?
Q1。假设我想要改变用户在按下主“删除”按钮之前标记为删除的每个“项目”的外观。(这种即时的视觉反馈应该消除了“你确定吗?”对话框的需要。)用户将选中复选框以指示应该删除哪些项。如果复选框未选中,则该项应恢复其正常外观。
应用或删除CSS样式的最佳方法是什么?
Q2。假设我想允许每个用户个性化我的站点的呈现方式。例如,从固定的字体大小中选择,允许用户自定义前景色和背景颜色等。
应用用户选择/输入的CSS样式的最佳方法是什么?
当前回答
下面是我如何有条件地在禁用按钮上应用灰色文本样式
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
styleUrls: [ './app.component.css' ],
template: `
<button
(click)='buttonClick1()'
[disabled] = "btnDisabled"
[ngStyle]="{'color': (btnDisabled)? 'gray': 'black'}">
{{btnText}}
</button>`
})
export class AppComponent {
name = 'Angular';
btnText = 'Click me';
btnDisabled = false;
buttonClick1() {
this.btnDisabled = true;
this.btnText = 'you clicked me';
setTimeout(() => {
this.btnText = 'click me again';
this.btnDisabled = false
}, 5000);
}
}
下面是一个工作示例: https://stackblitz.com/edit/example-conditional-disable-button?file=src%2Fapp%2Fapp.component.html
其他回答
有一件事要注意-如果CSS样式有破折号-你必须删除它们。所以如果你想设置background-color,正确的方法是:
ng-style="{backgroundColor:myColor}"
Angular提供了许多内置指令,用于有条件地/动态地操纵CSS样式:
ng-class - use when the set of CSS styles is static/known ahead of time ng-style - use when you can't define a CSS class because the style values may change dynamically. Think programmable control of the style values. ng-show and ng-hide - use if you only need to show or hide something (modifies CSS) ng-if - new in version 1.1.5, use instead of the more verbose ng-switch if you only need to check for a single condition (modifies DOM) ng-switch - use instead of using several mutually exclusive ng-shows (modifies DOM) ng-disabled and ng-readonly - use to restrict form element behavior ng-animate - new in version 1.1.4, use to add CSS3 transitions/animations
正常的“Angular方式”包括将模型/作用域属性绑定到接受用户输入/操作的UI元素上(即使用ng-model),然后将该模型属性关联到上面提到的内置指令之一。
当用户更改UI时,Angular会自动更新页面上的相关元素。
Q1听起来像是ng-class的一个很好的例子——CSS样式可以在类中捕获。
ng类接受一个“表达式”,该表达式必须求值为以下其中之一:
用空格分隔的类名字符串 类名数组 类名到布尔值的映射/对象
假设你的项目是使用ng-repeat在一些数组模型上显示的,并且当项目的复选框被选中时,你想应用pending-delete类:
<div ng-repeat="item in items" ng-class="{'pending-delete': item.checked}">
... HTML to display the item ...
<input type="checkbox" ng-model="item.checked">
</div>
上面,我们使用ng-class表达式类型#3 -类名到布尔值的映射/对象。
Q2听起来像是ng-style的一个好例子——CSS样式是动态的,所以我们不能为此定义一个类。
Ng-style接受一个必须求值为的“表达式”:
CSS样式名称到CSS值的映射/对象
举一个人为的例子,假设用户可以在文本框中输入一个颜色名称作为背景色(jQuery的颜色选择器会更好):
<div class="main-body" ng-style="{color: myColor}">
...
<input type="text" ng-model="myColor" placeholder="enter a color name">
为以上两种乐器演奏小提琴。
小提琴也包含了ng-show和ng-hide的例子。如果选中复选框,除了背景颜色变为粉红色之外,还会显示一些文本。如果在文本框中输入'red',则会隐藏一个div。
span class="circle circle-{{selectcss(document.Extension)}}">
和代码
$scope.selectcss = function (data) {
if (data == '.pdf')
return 'circle circle-pdf';
else
return 'circle circle-small';
};
css
.circle-pdf {
width: 24px;
height: 24px;
font-size: 16px;
font-weight: 700;
padding-top: 3px;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
background-image: url(images/pdf_icon32.png);
}
还有一种(将来)有条件地应用样式的方法是有条件地创建有作用域的样式
<style scoped type="text/css" ng-if="...">
</style>
但是现在只有FireFox支持范围样式。
从AngularJS v1.2 rc开始,ng-class甚至ng- atr -class在使用SVG元素时都失败了(它们在之前确实有效,即使是在class属性内部进行正常绑定)
具体来说,这些方法现在都不起作用:
ng-class="current==this_element?'active':' ' "
ng-attr-class="{{current==this_element?'active':' '}}"
class="class1 class2 .... {{current==this_element?'active':''}}"
作为一种变通办法,我不得不使用
ng-attr-otherAttr="{{current==this_element?'active':''}}"
然后使用样式
[otherAttr='active'] {
... styles ...
}