I have my angular.js app built with a php page that contains a variable that my app needs on load. The angular app on load then uses this variable to fetch more data using a POST. What is the best practice for passing a variable to angular? Also, how can I tell angular to not run the initial controller function until after it has the variable loaded? Thanks in advance.
            Asked
            
        
        
            Active
            
        
            Viewed 3,118 times
        
    0
            
            
        - 
                    possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – dm03514 Jun 15 '15 at 13:26
2 Answers
3
            In your case probably the best way is to write it down with something like this:
<script type="text/javascript">
    var _yourSpecialVar = <?php echo $variable ?>;
</script>
And in Angular:
$window._yourSpecialVar
EDIT:
For example in an Angular controller:
angular.module('app').controller('YourController', ['$scope', '$window', function($scope, $window) {
    $scope.myVar = $window._yourSpecialVar
}]);
 
    
    
        michelem
        
- 14,430
- 5
- 50
- 66
0
            
            
        Use ng-init to pass data from php to angular. It allows you to evaluate an expression in the current scope.
<div ng-init="<?PHP {name='John'}?>">
This directly goes to the scope.
 
    
    
        Charlie
        
- 22,886
- 11
- 59
- 90
