cari

Rumah  >  Soal Jawab  >  teks badan

angular.js - 怎样利用ng-repeat的$index选取不同的style

<!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>

这是测试代码,下面是测试效果图,请问怎样才能让字的颜色相应改变呢?

天蓬老师天蓬老师2744 hari yang lalu501

membalas semua(1)saya akan balas

  • 淡淡烟草味

    淡淡烟草味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>

    balas
    0
  • Batalbalas