Nicolas Talichet
Nicolas Talichet

Reputation: 311

Use an Angular JS value as a parameter of TWIG "path" function in Symfony 2

The {[{project.id}]} AngularJS variable is not interpreted by twig in the path function. This my code:

<section class="main" ng-controller="PaginationDemoCtrl">
    <table class="table table-striped table-hover table-responsive">
        <thead>
            <tr>
                <th>{% trans %}Id{% endtrans %}</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="project in filteredProjectList">
                <td>
                    <a href="{{ path('project_show', { 'projectId': {[{project.id}]} }) }}">
                        {[{ project.id }]}
                    </a>
                </td>
            </tr>
        </tbody>
    </table>

    <pagination
            total-items="totalItems" 
            items-per-page="itemsPerPage"
            ng-model="currentPage" 
            ng-change="pageChanged()">
    </pagination>
</section> 

<script>
    angular.module('myModule', ['ui.bootstrap']);
    angular.module('ui.bootstrap').config(function ($interpolateProvider) {
        $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
    });
</script>

Have you got a trick to fix this problem?

Upvotes: 1

Views: 2547

Answers (2)

Maerlyn
Maerlyn

Reputation: 34107

Angular is client-side, twig is server-side, so you cannot call twig's path function from angular.

If you want to generate routes on the client, look into FOSJsRoutingBundle.

Upvotes: 2

Davin Tryon
Davin Tryon

Reputation: 67296

Not sure what you are trying to do here, but you don't need to interpolate product.id again. This should be ok, but it would help to see your path function:

<tr ng-repeat="project in filteredProjectList">
    <td>
        <a ng-click="path('project_show', { 'projectId': project.id })">
            {[{ project.id }]}
        </a>
    </td>
</tr>

I'm assuming here that path is a function defined on the controller's $scope.

Upvotes: 0

Related Questions