在 Angular 2 / Ionic 2 中,更改对象颜色的一种简单方法是使用 theme/variables.scss 文件。
// Named Color Variables
// --------------------------------------------------
// Named colors makes it easy to reuse colors on various components.
// It's highly recommended to change the default colors
// to match your app's branding. Ionic uses a Sass map of
// colors so you can add, rename and remove colors as needed.
// The "primary" color is the only required color in the map.
$colors: (
primary: #488aff,
secondary: #32db64,
danger: #f53d3d,
light: #f4f4f4,
dark: #222,
my-special-color: #ffcc55,
);
现在要在 ionic2 页面中动态更改颜色,您可以通过将颜色绑定到 HTML 部分中的变量
来实现
<button [color]="myBtnColor">MyButton</button>
在你的 TypeScript 部分
import { ..., ChangeDetectorRef, ... } from '@angular/core';
...
export class MyComponent {
myBtnColor = "secondary"
constructor(private changeDetectorRef:ChangeDetectorRef) {}
...
function changeColorDynamicaly() {
myBtnColor = "my-special-color";
this.changeDetectorRef.detectChanges();
}
...
}
在我的例子中,ChangeDetectorRef 用于查看视图中反映的实际更改。视图已失效,无法更新。