2

In HTML:

<ul class="list-group">
   <li ng-repeat="item in simpleListVM.datasource track by $index" class="list-group-item">
      <simple-list-items listitem="item" listitemid="{{item.data.name + $index + 1}}"></simple-list-items>
   </li>
</ul> 

In Directive:

scope: {
    listitem: '=',               
    listitemid:'&'                   
}

When I run the application it shows error.

Syntax Error: Token '{' invalid key at column 2 of the expression [{{item.data.name +$index + 1}}] starting at [{item.data.name +$index + 1}}].

3
  • Is that the line the error points to? but it has an array in the error, but not in the question? Commented Feb 15, 2017 at 4:56
  • please create a demo with the issue you are facing will help understand you problem easily. Commented Feb 15, 2017 at 4:57
  • Yes simpleListVM.datasource is array Commented Feb 15, 2017 at 4:57

2 Answers 2

1

That is because the listitemid you provided is an interpolated expression and should be a function as defined by listitemid: '&'.

To me it seems you want to change listitemid: '&' to listitemid: '@'. This will bind to the result of the expression.

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

1 Comment

No problem @user2323308. Glad to help
0

Stephen J Barker is right, please check my example code,

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
</head>

<body ng-controller="MainCtrl">
  <ul class="list-group">
    <li ng-repeat="item in simpleListVM.datasource track by $index" class="list-group-item">
      <simple-list-items listitem="item" listitemid="{{item.name + $index + 1}}"></simple-list-items>
    </li>
  </ul>
</body>
<script>
  var app = angular.module('plunker', []);

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

    $scope.simpleListVM = {};
    $scope.simpleListVM.datasource = [{
      'name': 'test1'
    }, {
      'name': 'test2'
    }, {
      'name': 'test1'
    }];

  }).directive("simpleListItems", function() {
    var dir = {};
    dir.scope = {
      listitem: '=',
      listitemid: '@'
    };
    dir.link = function(s, e, a) {
      console.log("listitem", s.listitem);
      console.log("listitemid", s.listitemid);
    }
    return dir;
  });
</script>

</html>

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.