0

Ok i have no idea how to generate dynamic a links. Im creating a dynamic navbar

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

The problem is ng-click takes an expression. I cant pass there dynamic content from elem.

3 Answers 3

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

In controller

$scope.anch = functin(index) {
    $scope.anchor = elements[index];
}
Sign up to request clarification or add additional context in comments.

Comments

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

Try this

Comments

0

You can pass your elem to controller using by

 ng-click="action(elem)"

Please see sample below

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.