In General, you want to use AJAX calls to get values from your database to your angular app. But what about user authentication?
<?php session_start(); if (empty($_SESSION['uname'])){ header("location:auth/login.php"); } ?>
That's a pretty common piece of code. So, how do you pass the username to your Angular app?
Use ng-init when you set up your controller in the HTML. Use PHP to echo variables to the page.
<div class="container-fluid" ng-controller="appCtrl" ng-init="userInit('<?php echo $_SESSION['uname']."', '".$_SESSION['role'] ?>')" >
Now, create a userInit function in your angular controller that will handle the variables:
$scope.userInit = function(uid, role) { $scope.user = uid; $scope.role = role; // Get a list of projects for user $scope.projectList($scope.user); }
And it's that simple!
Thanks:) I was stuck on this for a while.
ReplyDeleteThanks for your comment... It's nice to know that it's helping someone.
DeleteAnother way is to pass in a global Javascript object as a var between script tags in the web page source (PHP page). and a service/factory picks up this value in the AngularJS space. Saves an HTTP request.
ReplyDeletethis is correct way actually..
DeleteDo you have an example demonstrating this? Would be much appreciated.
DeleteThanks for this.. just as jeremy said "I was stuck on this for a while".. :)
ReplyDeleteThx for this.
ReplyDeletewhen i am using php tag in html template got error message "parse syntax error"please help me to solve this error
ReplyDeletehey ,it wouldn't for me
ReplyDeletehey ,it wouldn't work for me
ReplyDeleteWhat's not working about it? Telling me it doesn't work doesn't help me help you.
Deletethank you so much bro :)
ReplyDeleteGood work
Casino | Online Gaming | Casino Software | drmcd
ReplyDeleteDiscover the best casino 동두천 출장마사지 software and slots games, bonuses 성남 출장샵 and 하남 출장마사지 more at drmcd. Get casino 구리 출장샵 software 영천 출장마사지 reviews, ratings, promotions, latest bonuses.