Laravel/Angular: passing data within php page to angularjs

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

When using Angular (or even just jQuery), I thank @JeffreyWay for this package:,https://github.com/laracasts/PHP-Vars-To-Js-Transformer, 1 This might help? github.com/laracasts/PHP-Vars-To-Js-Transformer – Laurence Jun 20 '14 at 12:53 ,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.

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>
?>
load more v
88%

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.,This currently works and i get my storeid value so i can then send to my factory to get the data.,and then in your Angular controller, you access that data in pure JS, without needing to juggle data attributes.,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:

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);
            ....
load more v
72%

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.,This currently works and i get my storeid value so i can then send to my factory to get the data.,You shouldn't use <?php ... ?> or link to PHP site or something like that in your Angular front-end, because once you decide to use Angular, its mean all your front-end is built with Angular, PHP is server side language. Just send request to server side, then receive data and display...,Seems that ngRepeat order the data by the $$hash, probably the workaround is to use a function that list the keys and then iterate over them:

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);
            ....
load more v
65%

To let Angular handle the frontend, we will need Laravel to pass our user to our index.php file. We can place this in a few different places. By default, when you use:,With our Angular Service done, let’s go into our controller and use it.,We now have our routes to handle the 3 main things our Laravel backend needs to do.,For our use cases, we will be using Laravel as the RESTful API backend and Angular as the frontend to create a very simple single-page comment application.

Let’s run the artisan command that will create our comments migration so that we can create the table in our database:

php artisan migrate: make create_comments_table--create = comments
load more v
75%

We will use Laravel to handle user registration, persisting user data to a database and providing some restricted data that needs authentication for the Angular app to consume. We will create an example API subdomain to simulate Cross-origin resource sharing (CORS) as well.,Bootstrapping the Angular Application,If the user is authenticated and the token is valid, we can safely return the restricted data to the frontend via JSON.,This is a simple service that makes requests to the authentication server as well as the API server for some dummy restricted data. It makes the request, and delegates success and error callbacks to the controller.

Here is a JWT token example:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwLCJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0.
yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw
load more v
40%

Example 2: We can pass an array with multiple values also.,Here are some of the ways we can pass data to the view:,How to get visitors country from their IP in PHP ?,PHP | strval() Function

web.php
Route::get('/', function() {
   return view('gfg', ['articleName' => 'Article 1']);
});
load more v

Other "undefined-undefined" queries related to "Laravel/Angular: passing data within php page to angularjs"