6

I want to show a laravel blade view file in angular JS directive by

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

commentsApp.directive('commentForm',function(){
    return {
        restrict: 'EA',
        replace: 'true'
        templateURL: 'views/comments/comment-form.blade.php'
    }
});

I want to use it by angular directive instead of @include('comments.comment-form') Where is my problem? How to solve this. Thank you.

1
  • give the laravel route thats all :) Commented Apr 14, 2015 at 6:59

3 Answers 3

13

First you must define a route in laravel

Route::get('comment-form', function() {
    return view('comments.comment-form');
});

Then you can use it in AngularJS

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

commentsApp.directive('commentForm', function() {
    return {
        restrict: 'EA',
        replace: 'true',
        templateURL: 'comment-form'
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

You could use wildcard for route to dynamically get views and end up with only one route for getting views.
How can you do that please?
0

Answer above is a good idea, however i dont like the idea of asking for a template by routing, We would create a route for each component :c . I leave my solution here:

  1. In gulpfile.js inside elixir function add this line:
 


     var elixir = require('laravel-elixir');
        elixir(function(mix) {
            mix.copy('resources/assets/js/angular/components/**/*.template.html', public/angular-templates'); 
//Find all files with suffix .template.html
    });



As you notice it, i created a folder called 'angular' and then another one called 'components', there we will have our components



    Angular----------------------------- 
    --Components------------------------ 
    ----my-component.directive.js------- 
    ----my-component.template.html------


  1. We have to create a global angular variable taking our browser window origin (www.myapp.com, localhost:8000, etc) by doing:


    angular.module('myModule',[])
    .value('pathAssets', location.origin + '/angular-templates/')


  1. In our templateUrl we will call the template by writting:


    templateUrl: pathAssets + '../angular-templates/my-template.html', 


I have to say we have to concat our angular files in a file, otherwise it won't work D: if you don't know how to do it, add these lines in your gulpfile.js



    mix.scripts([
        '../../../bower_components/angular/angular.min.js',
        'angular/app.js',
        'angular/controllers/**/*.controller.js',
        'angular/components/**/*.directive.js',
        'angular/bootstrap.js',
], 'public/js/app.js'); //We concatenate angular files saving them in app.js


  1. Finally execute the command 'gulp' in terminal(In our project), it should generate a new folder in public called angular-templates.

That's it :)

Comments

0
(function(angular) {
  'use strict';
angular.module('app').component('bringTeamToEvent', {
  templateUrl: '/assets/ng/app/team/bringTeamToEvent.html',
  bindings: {
    hero: '='
  }
});
})(window.angular);

Just work from the public directory, no need to compile assets and move if you dont need to.

Then add the @ symbol to tell blade to ignore and let angular do its work within the template

<span>Name: @{{ $ctrl.hero.name}}</span>

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.