Kirk
Kirk

Reputation: 16245

ng-click with a dynamic {{}} string

I have a HTML table that has the id set with a databind inside of a ng-repeat

<div class="project" ng-repeat="project in data.projects">
    <h2>
      {{projectState.name}}
    </h2>
    <table class="table" id="{{project.name + selectedType}}">
    ...
    </table>
    ...
</div>

This properly sets the id as expected, but I need to use this id in a ng-click call.

<button ng-click="export({{project.name + selectedType}})">
...
</button>

This produces the error when the page loads

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 16 of the expression [export({{project.name + selectedType}})] starting at [{project.name + selectedType}})].

How do I properly reference the databound id of {{project.name + selectedType}} in a ng-click?

Upvotes: 0

Views: 1063

Answers (4)

Syed Asad Abbas
Syed Asad Abbas

Reputation: 79

When you are using ng-click, it means you are already in angular scope so you don't need to use {{}} to call a function or evaluate the expression

Upvotes: 0

ferhado
ferhado

Reputation: 2604

angular.module('myApp',[])
.controller('myCtlrl',function($scope){
$scope.projects=[
  { id: 1, name: 'John', address: 'Highway 71'},
  { id: 2, name: 'Peter', address: 'Lowstreet 4'},
  { id: 3, name: 'Amy', address: 'Apple st 652'},
  { id: 4, name: 'Hannah', address: 'Mountain 21'},
  { id: 5, name: 'Michael', address: 'Valley 345'},
  { id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
  { id: 7, name: 'Betty', address: 'Green Grass 1'},
  { id: 8, name: 'Richard', address: 'Sky st 331'},
  { id: 9, name: 'Susan', address: 'One way 98'},
  { id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
  { id: 11, name: 'Ben', address: 'Park Lane 38'},
  { id: 12, name: 'William', address: 'Central st 954'},
  { id: 13, name: 'Chuck', address: 'Main Road 989'},
  { id: 14, name: 'Viola', address: 'Sideway 1633'}
];

$scope.export=function(id){
alert(id);
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtlrl">
  <div class="project">
      <table class="table" id="{{project.name + address}}" border=1>
      <tr><th>ExportButton</th><th>Id</th><th>Name</th><th>Address</th></tr>
         <tr ng-repeat="project in projects">
           <td><button ng-click="export(project.name+project.address)">Export</button></td>
           <td>{{project.id}}</td>
           <td>{{project.name}}</td>
           <td>{{project.address}}</td>
         </tr>
      </table>
  </div>
</div>

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

Pass project.name and selectedType as 2 arguments to export method :

<button ng-click="export(project.name,selectedType)">
...
</button>

and inside export method concatenate them:

$scope.export = function(name, type){
   var val = name + type;
   // ...
}

or just remove {{ }} from export

Upvotes: 1

Muhammad Usman
Muhammad Usman

Reputation: 10148

You don't need to give an expression inside of export(). This should be resolved using

export(project.name,selectedType)

Upvotes: 0

Related Questions