<!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像下面這樣寫: