AngularJS:如何在单击时将活动类设置为列出项目?三元方法

时间:2015-08-15 17:36:32

标签: javascript angularjs

我是AngularJS的新手,具有有限的Javascript经验。我想在单击该项目时将.active类设置为ul列表项。我正在使用ng-repeat指令列出项目。我怎样才能为我的例子写一个三元表达式? Full plnkr here

查看:

<li ng-repeat="item in list" ng-click="$index = item.id">
      <a href="#" ng-class="(item.id == $index) ? 'active' : ''">
        <div style="float: left;"><img src="http://placehold.it/100x100"></div>
        <h3 style="padding-left: 5em;">{{item.title}}</h3>
      </a>
</li>

控制器:

app.controller('TestCtrl', function($scope) {
  $scope.list = [
    {
      "id": 0,
      "title": "Batman",
      "plot": "some stupid plot of a movie."
    },
    {
      "id": 1,
      "title": "Superman",
      "plot": "another plot here."
    },
    {
      "id": 2,
      "title": "Xmen",
      "plot": "asdfadsf asdfasdfasd asdfasdfdsf."
    },
    {
      "id": 3,
      "title": "Avengers",
      "plot": "asdfasdf asdfdddd a3234323 dsfdf."
    }
    ]
});

如果有更简单的方法,请随时告诉我。但是,我喜欢三元组。谢谢!

0 个答案:

没有答案