Rumah > Soal Jawab > teks badan
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<h1 style="color:contents[$index].color" ng-repeat="x in records">{{x.name}}+ {{contents[$index].color}}</h1>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = [
{name:"T"},
{name:"D"},
{name:"Z"}
];
$scope.contents=[
{color:"red"},
{color:"blue"},
{color:"yellow"}
]
});
</script>
这是测试代码,下面是测试效果图,请问怎样才能让字的颜色相应改变呢?
淡淡烟草味2017-05-15 17:03:45
Gunakan arahan ng-style
terbina dalam, yang menerima objek sebagai parameter.
Jadi, js tidak perlu diubah, html ditulis seperti berikut:
<h1 ng-style="contents[$index]" ng-repeat="x in records">{{x.name}} + {{contents[$index].color}}</h1>