Angular ng-click动态内容

时间:2014-10-15 07:10:59

标签: angularjs

好吧我不知道如何生成动态链接。我正在创建一个动态导航栏

<div ng-repeat="elem in elements">
<div ng-class="{ 'active' : anchor=={{elem}} }">
<a ng-click="anchor = elem">{{elem}}</a>
</div>

问题是ng-click需要一个表达式。我无法从elem那里传递动态内容。

3 个答案:

答案 0 :(得分:4)

<a ng-click="anch($index)">{{elem}}</a>

在控制器中

$scope.anch = functin(index) {
    $scope.anchor = elements[index];
}

答案 1 :(得分:0)

<ul>
  <li ng-repeat="item in items">
    <a ng-click="upload_variant_image()">{{item.name}}</a>
  </li>
</ul>

试试这个

答案 2 :(得分:0)

您可以使用

elem传递给控制器
 ng-click="action(elem)"

请参阅下面的示例

var app = angular.module('app', []);

app.controller('fCtrl', function($scope) {

  $scope.elements = [1, 2, 3, 4];

  $scope.action = function(elem) {
    $scope.anchor = elem

  }

  $scope.anchor = 1;

});
.active {
  color: red
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app">
  <div ng-controller="fCtrl">
    <div ng-repeat="elem in elements">
      <div ng-class="{ 'active' : anchor=={{elem}} }">
        <a ng-click="action(elem)">{{elem}}</a>
      </div>
    </div>
  </div>