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

标签 css angularjs

我有一个正在处理的表单页面,基本的 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/

相关文章:

jQuery Cycle IE7+8 问题 - JPEG 上出现白色标记

jquery - 滚动模态,而不是正文

javascript - 如何在浏览器调整大小时保持图像和表格单元格的大小相对

CSS3 flexbox调整垂直对齐元素的高度

javascript - 如何创建本地 MongoDB _id

javascript - 文本区域 Shift+Enter 进入下一行并 Enter 提交表单

javascript - OrderBy 在 AngularJS 中没有按预期工作

javascript - AngularJS指令对象有 "No Method"

javascript - Angular/nodejs ng-file-upload - 文件在 Angular 上加载但在 Nodejs 端丢失

javascript - 存储总数并在提交时累积