0

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?

4
  • this button is inside your table right ? Commented Sep 26, 2017 at 17:39
  • it's inside the div Commented Sep 26, 2017 at 17:40
  • ng-click expects an angular expression. The mustaches shouldn't be there: ng-click="export(project.name + selectedType)" Commented Sep 26, 2017 at 17:41
  • Are you trying to pass a reference to the element into the export() function? Commented Sep 26, 2017 at 17:44

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That did it with export(project.name + selectedType) but I like your method more.
0

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

export(project.name,selectedType)

Comments

0

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>

Comments

0

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

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.