Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

My URL contains a query string, for example "www.website.php?feature1=true&feature2=false".

When my page loads, I want angular variables set to the value of the query string variables like this:

$scope.feature1 = $_POST['feature1'];
$scope.feature2 = $_POST['feature2'];

How can I do this? Thanks.

share|improve this question
1  
The values that you passing are through query string, they are not posted data. – randominstanceOfLivingThing yesterday

You can access GET parameters (URL parameters) through $routeParams:

app.controller('CtrlName', ['$scope','$routeParams', function($scope, $routeParams) {
  $scope.feature1 = $routeParams.feature1;
  $scope.feature2 = $routeParams.feature2;
}]);
share|improve this answer

Assuming your web entrypoint is a php page (or any other serverside rendered html page, such as a ASP.NET MVC View), the way I usually get values like these from the server into angular is using the constant function, and place it in the .php page right after you reference your angular app. Something like this:

<script src="app.js" />
<script>
    angular
        .module('app')
        .constant("myConfig", {
            "feature1": "<?php echo htmlspecialchars($_POST['feature1']); ?>",
            "feature2": "<?php echo htmlspecialchars($_POST['feature2']); ?>"
        });
</script>

Then in your controller, you just inject the constant: app.controller('Controller', ['myConfig', function(myConfig) {..}

While there are more convenient ways than this, when you're passing query strings (that can be accesses by JavaScript), with this approach you can pretty much pass in anything from server side code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.