gpt4 book ai didi

javascript - ng-model 可以附加到样式标签吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:12 24 4
gpt4 key购买 nike

我想使用 angularjs 创建一个基本的模板编辑器,我想知道是否可以使用 ng-model 创建一个输入字段,并将该变量连接到样式标签以控制整个页面上的 css在具有内联样式的特定元素上使用它?有可能做这样的事情吗?

<!doctype html>
<html lang="en" ng-app>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="js/angular.min.js"></script>
<style>
html, body { height:100%; margin:0; padding:0; }
div, ul, li, ol, span { margin:0; padding:0; }

</style>
</head>
<body>
<textarea type="text" ng-model="data.style" placeholder="enter style"></textarea>
</body>
</html>
<script>

window.addEventListener('load', function(){

var style = document.createElement('style');
var textnode = document.createTextNode('{{data.style}}');
style.appendChild(textnode);

document.querySelector('head').appendChild(style);


}, false)

</script>

最佳答案

您可以像这样按需加载整个样式表:

HTML

<!DOCTYPE html>
<html ng-app="myApp" ng-controller="myCtrl">

<head>
<script data-require="angular.js@1.2.4" data-semver="1.2.4" src="http://code.angularjs.org/1.2.4/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" ng-href="{{styleHref()}}" />
<script src="script.js"></script>
</head>

<body >
<div class="box"></div>
<textarea ng-model="data.style"></textarea>
</body>
</html>

JS

angular.module('myApp', [
]).
controller('myCtrl',function($scope) {
$scope.data = { style: "style-1" }; // can come from init func, http call, etc
$scope.styleHref = function() { return $scope.data.style + ".css"; };
});

参见 this plunkr as an example

关于javascript - ng-model 可以附加到样式标签吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20572110/

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