【发布时间】:2017-03-01 03:11:49
【问题描述】:
我正在使用 Angular 2 创建一个非常简单的 Web 应用程序,它要求用户输入 RGB 颜色代码。用户输入代码后,页面背景应变为输入的颜色。
目前,这是我在 app.component.ts 文件中的内容:
/**
* Fired when the user clicks the #update-background button. This function will read the current input values and set the background color to these values.
*
* @param rgb: RGB object bound to the input fields. Holds the rgb(,,) values for the new background
*/
updateBackgroundColor(rgb) {
// 1. Construct the new background color
var updatedBackgroundColor = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")";
// 2. Set the background of the <body> to the new background color. Right now I am using direct DOM manipulation because I could not find a way to access the <body> via typescript.
document.body.style.background = updatedBackgroundColor;
}
这是更新<body> 元素样式的最佳方式吗?代码目前正在运行 (demo),但我只想确定这是通过 typescript 和 Angular 2 访问 <body> 元素的最有效方式。
【问题讨论】:
标签: css angular angular2-directives