Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following laravel html/blade page and i want to pass in a storeid that then can get picked up via angular to then use to get and do some workings with.

Below is how i am accomplishing it NOW but is there a better way to produce the same?

Blade Partial

<div ng-controller="storeChartCtrl" data-storeid="{{ $store['id'] }}">
    <p>{{ $store['name'] }} Sales</p>
    ...
</div>

Angular Ctrl

.controller('storeChartCtrl', ['$scope', 'Store', function($scope, Store){
    //-- get the requested store sales
    var storeid = JQ('#storeChart').data('storeid');

    $log.info(storeid);
    ....

This currently works and i get my storeid value so i can then send to my factory to get the data.

share|improve this question
1  
    
Ah the good old laracasts to js method, nice. Will consider but seems a little overkilll for that data (regret saying that as app gets bigger). Thanks will probably apply –  Simon Davies Jun 20 at 13:00
    
@TheShiftExchange add this as an answer so i can mark it, worked, don't know why i did not think of this from the start :-) –  Simon Davies Jun 20 at 13:15

2 Answers 2

up vote 1 down vote accepted

When using Angular (or even just jQuery), I thank @JeffreyWay for this package:

https://github.com/laracasts/PHP-Vars-To-Js-Transformer

share|improve this answer

Ultimately, you are rendering an HTML page from the server with certain data embedded; you can either embed data inside your HTML, or you can "render" an object full of data in the page like so:

<?php
$value = "Hello";
$bob = "Bob";

// template code

<script type="text/javascript">
myData = {             // make it global for easy access
    blah: $value,
    alice: $bob
};
</script>
?>

and then in your Angular controller, you access that data in pure JS, without needing to juggle data attributes.

.controller('storeChartCtrl', ['$scope', 'Store', function($scope, Store){
    console.log(myData);
})]);
share|improve this answer
    
Thank for this it seems adding to the global scope etc like yours and the laracasts php vars to js is one way to go, especially when there is the requirement for more than more a few items to pass and the data attr is then not fesible, thasnk again –  Simon Davies Jun 20 at 13:03
    
Thanks went with the laravel implementation method as suited best, but in simply terms it does a similar thing. Thaksn for the reply though –  Simon Davies Jun 20 at 13:16
    
This method is totaly ok. I recommend namespacing your injected data to reduce overall global scope polution. –  DrDyne Jun 20 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.