gpt4 book ai didi

css - AngularJS:如何为所有元素设置样式属性?

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:09 27 4
gpt4 key购买 nike

我有一个正在处理的表单页面,基本的 AngularJS 结构正在运行。当用户选择特定字体时,我希望所有页面元素都更改为该字体。在我的样式表中,我使用的是“*”选择器,但我不知道如何通过 Angular 更新它。我在下面附上了基本的页面结构代码(请注意,该页面的元素比这更多,但我删除了这个问题中除 H1 之外的所有元素)。有什么想法吗?

<!doctype html>
<html ng-app="theapp">
<head>
<title>App</title>
<style type="text/css">
* {
font-family: sans-serif;
font-size: 12pt;
}
</style>
</head>
<body ng-controller="FormController as form">
<h1>Hello</h1>
<form name="theForm">
<select ng-model="font" ng-options="font.face for font in fonts"></select>
</form>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript">
(function () {
angular.module("theapp", [])
.controller("FormController", ["$scope", function ($scope) {
$scope.fonts = [
{ face: "sans-serif" },
{ face: "Arial" },
{ face: "Tahoma" },
{ face: "Trebuchet MS" },
{ face: "Verdana" },
{ face: "serif" },
{ face: "Times" },
{ face: "Georgia" },
{ face: "monospace" },
{ face: "Courier" }
];
$scope.font = $scope.fonts[0];
}]);
})();
</script>
</body>
</html>

最佳答案

您可以使用 ng-style在您的标记中并将其链接到 scope.font变量。

查看文档:https://docs.angularjs.org/api/ng/directive/ngStyle

我没试过,但你可以添加 divbody下这样 <div ng-style="style">在你的 Controller 中你做 $scope.style = {"font-family": $scope.font} .

关于css - AngularJS:如何为所有元素设置样式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com