{"id":586,"date":"2024-12-28T17:45:50","date_gmt":"2024-12-28T09:45:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=586"},"modified":"2024-12-28T17:45:50","modified_gmt":"2024-12-28T09:45:50","slug":"%e5%9c%a8-angularjs-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e6%9d%a1%e4%bb%b6-css-%e6%a0%b7%e5%bc%8f%e7%9a%84%e5%ba%94%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/28\/%e5%9c%a8-angularjs-%e4%b8%ad%ef%bc%8c%e6%80%8e%e6%a0%b7%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e6%9d%a1%e4%bb%b6-css-%e6%a0%b7%e5%bc%8f%e7%9a%84%e5%ba%94%e7%94%a8%ef%bc%9f\/","title":{"rendered":"\u5728 AngularJS \u4e2d\uff0c\u600e\u6837\u5b9e\u73b0\u52a8\u6001\u6761\u4ef6 CSS \u6837\u5f0f\u7684\u5e94\u7528\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 AngularJS \u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u4ee4\uff08Directives\uff09\u5b9e\u73b0\u52a8\u6001\u6761\u4ef6 CSS \u6837\u5f0f\u7684\u5e94\u7528\u3002\u901a\u5e38\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u6765\u6839\u636e\u6761\u4ef6\u63a7\u5236 CSS \u6837\u5f0f\uff1a<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>ng-class<\/code> \u6307\u4ee4<\/strong><\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>ng-style<\/code> \u6307\u4ee4<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\u6761\u4ef6 CSS \u6837\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>ng-class<\/code> \u6307\u4ee4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ng-class<\/code> \u7528\u4e8e\u6839\u636e\u6761\u4ef6\u52a8\u6001\u6dfb\u52a0\u548c\u79fb\u9664 CSS \u7c7b\u3002\u5982\u679c\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u5e94\u7528\u4e0d\u540c\u7684\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5728 HTML \u4e2d\u76f4\u63a5\u4f7f\u7528 <code>ng-class<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u6839\u636e\u6761\u4ef6\u5207\u6362\u7c7b\u540d<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html ng-app=\"myApp\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"styles.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body ng-controller=\"MainCtrl\"&gt;\n    &lt;div ng-class=\"{'highlight': isHighlighted, 'dim': !isHighlighted}\"&gt;\n      This text will change style based on condition.\n    &lt;\/div&gt;\n    &lt;button ng-click=\"toggleHighlight()\"&gt;Toggle Highlight&lt;\/button&gt;\n  &lt;\/body&gt;\n\n  &lt;script src=\"https:\/\/code.angularjs.org\/1.8.2\/angular.min.js\"&gt;&lt;\/script&gt;\n  &lt;script&gt;\n    angular.module('myApp', &#91;])\n      .controller('MainCtrl', function($scope) {\n        $scope.isHighlighted = false;\n\n        $scope.toggleHighlight = function() {\n          $scope.isHighlighted = !$scope.isHighlighted;\n        };\n      });\n  &lt;\/script&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ng-class=\"{'highlight': isHighlighted, 'dim': !isHighlighted}\"<\/code> \u6839\u636e <code>isHighlighted<\/code> \u7684\u503c\u52a8\u6001\u5e94\u7528 <code>highlight<\/code> \u7c7b\u6216 <code>dim<\/code> \u7c7b\u3002<\/li>\n\n\n\n<li>\u6309\u94ae\u901a\u8fc7 <code>ng-click<\/code> \u7ed1\u5b9a\u4e86\u4e00\u4e2a\u65b9\u6cd5 <code>toggleHighlight()<\/code>\uff0c\u8be5\u65b9\u6cd5\u5207\u6362 <code>isHighlighted<\/code> \u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">CSS\uff08styles.css\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>.highlight {\n  background-color: yellow;\n}\n\n.dim {\n  background-color: gray;\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>ng-style<\/code> \u6307\u4ee4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ng-style<\/code> \u5141\u8bb8\u4f60\u76f4\u63a5\u4e3a\u5143\u7d20\u52a8\u6001\u5e94\u7528\u5185\u8054 CSS \u6837\u5f0f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>ng-style<\/code> \u6839\u636e\u6761\u4ef6\u52a8\u6001\u8bbe\u7f6e\u6837\u5f0f\u7684\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u6839\u636e\u6761\u4ef6\u4fee\u6539\u5185\u8054\u6837\u5f0f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html ng-app=\"myApp\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"styles.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body ng-controller=\"MainCtrl\"&gt;\n    &lt;div ng-style=\"getDynamicStyles()\"&gt;This text will have dynamic styles.&lt;\/div&gt;\n    &lt;button ng-click=\"toggleHighlight()\"&gt;Toggle Highlight&lt;\/button&gt;\n  &lt;\/body&gt;\n\n  &lt;script src=\"https:\/\/code.angularjs.org\/1.8.2\/angular.min.js\"&gt;&lt;\/script&gt;\n  &lt;script&gt;\n    angular.module('myApp', &#91;])\n      .controller('MainCtrl', function($scope) {\n        $scope.isHighlighted = false;\n\n        $scope.toggleHighlight = function() {\n          $scope.isHighlighted = !$scope.isHighlighted;\n        };\n\n        $scope.getDynamicStyles = function() {\n          return {\n            'background-color': $scope.isHighlighted ? 'yellow' : 'gray',\n            'font-size': '20px',\n            'padding': '10px'\n          };\n        };\n      });\n  &lt;\/script&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ng-style=\"getDynamicStyles()\"<\/code> \u52a8\u6001\u8ba1\u7b97\u5e76\u5e94\u7528\u5185\u8054\u6837\u5f0f\uff0c<code>getDynamicStyles()<\/code> \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6837\u5f0f\u7684\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>toggleHighlight()<\/code> \u5207\u6362 <code>isHighlighted<\/code> \u7684\u503c\uff0c\u4ece\u800c\u52a8\u6001\u6539\u53d8 <code>background-color<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">CSS\uff08styles.css\uff09\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* \u4e0d\u9700\u8981\u7279\u6b8a\u7684 CSS\uff0c\u6837\u5f0f\u5b8c\u5168\u7531 ng-style \u52a8\u6001\u8bbe\u7f6e *\/<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u7ed3\u5408 <code>ng-class<\/code> \u548c <code>ng-style<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u4f7f\u7528 <code>ng-class<\/code> \u548c <code>ng-style<\/code>\u3002<code>ng-class<\/code> \u7528\u6765\u5904\u7406\u7c7b\u7684\u5e94\u7528\uff0c\u800c <code>ng-style<\/code> \u7528\u6765\u5904\u7406\u5185\u8054\u6837\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u540c\u65f6\u4f7f\u7528 <code>ng-class<\/code> \u548c <code>ng-style<\/code><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html ng-app=\"myApp\"&gt;\n  &lt;head&gt;\n    &lt;link rel=\"stylesheet\" href=\"styles.css\"&gt;\n  &lt;\/head&gt;\n  &lt;body ng-controller=\"MainCtrl\"&gt;\n    &lt;div ng-class=\"{'highlight': isHighlighted}\" ng-style=\"{'font-size': fontSize}\"&gt;\n      This text will change style and class based on condition.\n    &lt;\/div&gt;\n    &lt;button ng-click=\"toggleHighlight()\"&gt;Toggle Highlight&lt;\/button&gt;\n    &lt;button ng-click=\"increaseFontSize()\"&gt;Increase Font Size&lt;\/button&gt;\n  &lt;\/body&gt;\n\n  &lt;script src=\"https:\/\/code.angularjs.org\/1.8.2\/angular.min.js\"&gt;&lt;\/script&gt;\n  &lt;script&gt;\n    angular.module('myApp', &#91;])\n      .controller('MainCtrl', function($scope) {\n        $scope.isHighlighted = false;\n        $scope.fontSize = '20px';\n\n        $scope.toggleHighlight = function() {\n          $scope.isHighlighted = !$scope.isHighlighted;\n        };\n\n        $scope.increaseFontSize = function() {\n          $scope.fontSize = '30px';\n        };\n      });\n  &lt;\/script&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ng-class=\"{'highlight': isHighlighted}\"<\/code> \u6839\u636e <code>isHighlighted<\/code> \u503c\u52a8\u6001\u5e94\u7528 <code>highlight<\/code> \u7c7b\u3002<\/li>\n\n\n\n<li><code>ng-style=\"{'font-size': fontSize}\"<\/code> \u6839\u636e <code>fontSize<\/code> \u53d8\u91cf\u52a8\u6001\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7 <code>toggleHighlight()<\/code> \u548c <code>increaseFontSize()<\/code> \u65b9\u6cd5\u52a8\u6001\u6539\u53d8\u6837\u5f0f\u548c\u7c7b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ng-class<\/code><\/strong> \u7528\u4e8e\u6839\u636e\u6761\u4ef6\u52a8\u6001\u6dfb\u52a0\u6216\u79fb\u9664 CSS \u7c7b\u3002\u8fd9\u662f\u63a7\u5236\u7c7b\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u6539\u53d8\u7c7b\u7684\u6837\u5f0f\u3002<\/li>\n\n\n\n<li><strong><code>ng-style<\/code><\/strong> \u7528\u4e8e\u52a8\u6001\u8bbe\u7f6e\u5185\u8054\u6837\u5f0f\u3002\u5b83\u5141\u8bb8\u4f60\u76f4\u63a5\u5728\u5143\u7d20\u4e0a\u8bbe\u7f6e\u6837\u5f0f\uff0c\u5e76\u6839\u636e\u6761\u4ef6\u52a8\u6001\u66f4\u65b0\u5b83\u4eec\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u5408\u4f7f\u7528 <code>ng-class<\/code> \u548c <code>ng-style<\/code><\/strong>\uff1a\u5728\u9700\u8981\u540c\u65f6\u63a7\u5236\u7c7b\u548c\u5185\u8054\u6837\u5f0f\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u7ed3\u5408\u8fd9\u4e24\u8005\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6837\u5f0f\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728 AngularJS \u4e2d\u5b9e\u73b0\u7075\u6d3b\u7684\u6761\u4ef6 CSS \u6837\u5f0f\u63a7\u5236\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 AngularJS \u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6307\u4ee4\uff08Directives\uff09\u5b9e\u73b0\u52a8\u6001\u6761\u4ef6 CSS \u6837\u5f0f\u7684\u5e94\u7528\u3002\u901a\u5e38\u4f7f\u7528\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-586","post","type-post","status-publish","format-standard","hentry","category-css"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=586"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/586\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/586\/revisions\/587"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}