0

I'm struggling here (I am new to programming still), I have an ng-repeat that is repeating a list (page on Pages) which displays the correct number on the front end (i.e. 1) but, when I try and use the same for the ng-click, it will throw this error:

Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 31 of the expression [getCtrlScope().currentPage = {{ page }}] starting at [{ page }}].

What is strange is that it will display it correctly within the HTML code correctly but will not function.

Code:

<li class="page-item" ng-repeat="page in Pages">
    <a class="page-link" ng-click="getCtrlScope().currentPage = {{ page }}">{{ page }}</a>
</li>

What shows when rendered in HTML:

<li class="page-item ng-scope" ng-repeat="page in Pages">
   <a class="page-link waves-effect ng-binding" ng-click="getCtrlScope().currentPage = 2">2</a>
</li>

JS

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

MyApp.controller('MyCtrl', function ($scope, $filter) {
    $scope.Users = tJSONData;
    $scope.currentPage = 0;
    $scope.pageSize = 3;
    $scope.search = '';
    $scope.Pages = [1, 2, 3];

    $scope.getData = function () {
        return $filter('filter')($scope.Users, $scope.search)
    }

    $scope.updatePage = function() {
        $scope.$apply(function () {
            $scope.Users = tJSONData
        })
    }

    $scope.numberOfPages = function () {
        return Math.ceil($scope.getData().length / $scope.pageSize);
    }

    $scope.getCtrlScope = function () {
        return $scope;
    }
});

MyApp.filter('startFrom', function () {
    return function (input, start) {
        start = +start; //parse to int
        return input.slice(start);
    }
});

I've looked around but pretty much all the fixes have been by removing {{ }}, some it has worked for because it was rendering incorrectly but it seems mine is rendering correctly.

If anyone has any ideas, it'd be a great help :)

3
  • 2
    you don't need {{}} in ng-xx directives expressions. Commented May 24, 2017 at 15:43
  • @Pengyy Really gonna be honest, I have tried that but it didn't work before, it did now, I have been on this for a good few hours, Thanks :) Want to make it an answer so I can give credit to you? Commented May 24, 2017 at 15:45
  • use only currentPage instead of getCtrlScope().currentPage Commented May 24, 2017 at 15:51

1 Answer 1

1

You can access scope variables directly from view so you don't need getCtrlScope() function. Also all variables inside angular directives are scopes so you don't need brackets {{}}

Try this

<li class="page-item" ng-repeat="page in Pages">
    <a class="page-link" ng-click="currentPage = page">{{ page }}</a>
</li>
Sign up to request clarification or add additional context in comments.

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.