php – How to use Angular JS and Laravel 4 without conflicting with blade?

Question:

I would like to know how do I configure Laravel 4 to use AngularJs without conflicting with Blade, since the interpolation tags are the same?

Answer:

For you not to conflict, you will have to change the internal processing tags of Laravel's BladeCompiler . Or depending on the case, you can change the AngularJS properly.

LARAVEL BLADE

You can do this using the facade class for BladeCompiler , called Blade (which is an alias, you can check it out in app.php ).

In your application's app/start/global.php file, do the following code inclusion

Blade::setContentTags('[[', ']]');

Blade::setEscapedContentTags('[[[', ']]]');

Note : It is worth remembering that Blade has a cache of compiled views (according to the modification date of your blade view). So what was compiled before that needs to be reprocessed, so it doesn't get the data before the Blade's opening and closing tags are changed.

In Laravel 5 you can use the php artisan view:clear command to clear the views that were compiled in the old tween.

ANGULAR JS

If it is cumbersome or you don't want to change the BladeCompiler syntax, you can change the AngularJS tween tags. You can use the following method:

var myApp = angular.module('myApp', [], function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});

Update

It is possible in Laravel to escape the parsing tags {{ }} using the at sign ( @ ). So, since angular interprets these keys in the DOM, then you could do it like this:

 {{ $codigo_laravel }}

 @{{ codigo.angular }}

Another example:

<input type="name" data-id="@{{ user.id }}" />
Scroll to Top