0

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.

2
  • 1
    The values that you passing are through query string, they are not posted data. Commented Dec 30, 2016 at 23:49
  • Please mark an answer as "accepted" so this question is resolved. :) Commented Jan 16, 2017 at 22:40

2 Answers 2

2

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

Comments

0

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.

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.