从PHP传递数据到angular.js
问题描述:
我的angular.js应用程序是用php页面构建的,其中包含我的应用程序在加载时需要的变量。加载时的角度应用程序然后使用此变量使用POST获取更多数据。将变量传递给角度的最佳做法是什么?另外,如何在加载变量之前告诉angular不运行初始控制器函数?提前致谢。
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.
答
在你的情况下,最好的方法是用这样的东西写下来:
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>
在Angular中:
$window._yourSpecialVar
编辑:
例如在Angular控制器中:
For example in an Angular controller:
angular.module('app').controller('YourController', ['$scope', '$window', function($scope, $window) {
$scope.myVar = $window._yourSpecialVar
}]);