0

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.

1

2 Answers 2

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
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

is the angular part inside the controller file?
where you want, just inject $window
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.