简体   繁体   中英

How can i get the value of a variable from PHP to AngularJS?

=== Lets just say i already have setup my App and Controller ====

test.phtml

 $var = "Welcome";

myangular.js

$scope.var = "<?php $var; ?>" //not sure about this part

console.log(var)

MY PROBLEM is that, i cant display the console.log(var) it says "undefined?"

Is it possible? i want to pass the PHP VARIABLE to ANGULAR onload, right when page is loaded..

BTW im using phalcon framework

you need to echo the variable:

$scope.var = "<? echo $var; ?>"

or in a shorter way:

$scope.var = "<?=$var; ?>"

technicly both are the same.

I would suggest exposing a public API from your PHP application and then calling it using the $http service in angular-js.

$http.get('APIURL', {cache: true})
     .success(function(data){...})
     .error(function(data){});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM