<!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>
这是测试代码,下面是测试效果图,请问怎样才能让字的颜色相应改变呢?
使用内置的
ng-style
指令,这个指令接受一个对象作为参数。所以,js不用更改,html像下面这样写: