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

【字号: 日期:2023-01-13浏览:22作者:雯心

问题描述

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

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

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

问题解答

回答1:

使用内置的ng-style指令,这个指令接受一个对象作为参数。

所以,js不用更改,html像下面这样写:

<h1 ng- ng-repeat='x in records'>{{x.name}} + {{contents[$index].color}}</h1>

相关文章: