0

Let's say I have a HTML file that has AngularJS loaded in with 'ngRoute'.

Then I'm initialize the app by making an index.php file (I'm not displaying the basic HTML tags + angular JS includes here):

<?php $path = 'www.test.com' ?>

<div ng-app="testApp" ng-init="path='<?php echo $path ?>">
   <div ng-view></div>    
</div>

I'm trying to pass the PHP variable to my AngularJS App Config, by this:

// Module
var test = angular.module('testApp', ['ngRoute']);

// Routes
test.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: $rootScope.path + '/pages/display.htm',
        controller:  'testController'
    }) 
});


// Controllers
test.controller('testController', ['$scope', function($scope) {

}]);

This does not work though because the $rootScope is not definied. Can anyone tell me what am I missing here?

3 Answers 3

3

Based on comments about wanting to keep php and scripts separate just add a script tag with a variable that you can then access from anywhere in angular

<script>
     var myPathVar = '<?= $someVar; ?>';
</script>

Then you can do anything you want with inside angular such as add it as constant , pass it to $rootScope in your config or whatever

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you I've used this approach. Yeah basically if there's no other workaround, I'm doing this. Cool! Cheers for the answer!
no real need for a workaround. Sure it can be argued it's a global but one global can be tolerated with minimal impact potential
WARNING This code is probably vulnerable to cross-site scripting attacks. Do not use this code unless you want your site to be hacked. See Everything You Need to Know About Preventing Cross-Site Scripting Vulnerabilities in PHP for an introduction to the topic.
3

You can define a constant in a script tag in the page generated with PHP:

<script type="text/javascript">
    angular.module('testApp').constant('path', '<?php echo $path ?>');
</script>

And in your app config:

// Routes
test.config(function($routeProvider, path) {
    $routeProvider
    .when('/', {
        templateUrl: path + '/pages/display.htm',
        controller:  'testController'
    }) 
});

If path contains any user-supplied content, make sure to escape it appropriately for echoing as a javascript string.

1 Comment

This may not work if the JS code is wrapped in a function( $ ) {$(document).ready( ... );} )(jQuery); type block then you will have to attached it to something global like the window.
-1

you might want do do this instead

// Controllers
test.controller('testController', ['<?php echo $scope; ?>', function($scope) {

}]);

I dont use angularJs, but regardless '$scope' would literately be the word $scope

6 Comments

Thank you for your answer. What can I do if my Angular app is in a separate .js file?
Pass it to it somehow, hard to answer without knowing how you combine them, also, just as a practice I would avoid using the $ for JS variables, as it is confusing when combined with PHP.
What would be the point of this? This does not help get OP's php $path data into AngularJS's scopes
@PatrickEvans - well with all $ signed variables, and whatnot ( incomplete example ) I just picked the most obvious issue because in either JS or PHP '$scope' is just a word and not a variable.
@ArtisiticPhoenix unfortunately you can't inject external variables into angular components without registering them ..and $scope is protected in angular also
|

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.