加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

样式元素中的AngularJS无法正常工作

发布时间:2020-12-17 17:47:39 所属栏目:安全 来源:网络整理
导读:我正在尝试在样式元素中使用角度js绑定,但它似乎不起作用.这是一个错误,还有更好的方法吗? style ng-repeat="test in styles".test { background-color: {{ test.backgroundColor }}; display: {{ test.display }}; width: {{ test.width }}; height: {{ te
我正在尝试在样式元素中使用角度js绑定,但它似乎不起作用.这是一个错误,还有更好的方法吗?

<style ng-repeat="test in styles">
.test {
    background-color: {{ test.backgroundColor }};
    display: {{ test.display }};
    width: {{ test.width }};
    height: {{ test.height }};
    }
</style>

http://jsfiddle.net/cmsanche/PpyVn/

解决方法

我不认为Angular会分析样式元素.您可能想要使用ngStyle指令:

http://docs.angularjs.org/api/angular.module.ng.$compileProvider.directive.ngStyle

<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/angular-1.0.0rc10.min.js"></script>
</head>
<body>
<div ng-init="styles=[{backgroundColor: 'red',width:'100px',height: '100px',display: 'block'}]">

<div ng-repeat="test in styles" ng-style="test">
.test {
    background-color: {{ test.backgroundColor }};
    display: {{ test.display }};
    width: {{ test.width }};
    height: {{ test.height }};
    }
</div>
</html>

Plunker(像jsFiddle,但更像Angular友好)Demo

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读