Search code examples
angularjscurly-braces

How to calculated angular variable in braces


I have this code:

    <pre>totalItems: {{vm.pagination.totalItems}}</pre>
    <pre>items-per-page: {{vm.pagination.itemsPerPage}}</pre>
    <pre>Total: Math.ceil({{vm.pagination.totalItems / vm.pagination.itemsPerPage}})</pre>

Output:

enter image description here

I'm using Math.ceil() so the answer should be 11 but I'm not getting the correct answer as below:

enter image description here

How can I calculate angular value in braces? Please help and thanks in advance.


Solution

  • The best way is to create a scoped function:

    <pre>Total: {{calculateTotal(vm.pagination.totalItems, vm.pagination.itemsPerPage)}}</pre>
    

    Then add the function to the scope:

    $scope.calculateTotal = function(totalItems, itemsPerPage) {
      return Math.ceil(totalItems/itemsPerPage);
    };