Laravel/Angular: passing data within php page to angularjs

别等时光非礼了梦想. 提交于 2019-12-04 14:44:04
Laurence

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

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

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);
})]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!