本文介绍了将PHP变量传递给AngularJS变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用AngularJS数据绑定,我想以用户类型 num2
返回 num1 * num2
; num1
是一个PHP变量(用户无法更改).接下来,如何将PHP变量传递给有角js?a>,我尝试过:
Using AngularJS data binding, I'd like to return num1 * num2
as user type num2
; num1
is a PHP variable (cannot be changed by user). Following, How do I pass PHP variables to angular js?, I tried:
<!doctype html>
<html>
<head>
<title>AngularJS App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>
<body>
<? php $num1 = 5;?>
<div ng-app="myApp" ng-controller="myCtrl">
<form action="/action_page.php">
<div id="num1">
Num1: <div ng-model="num1" ng-init="num1('<?php echo $num1?>')">
</div>
Num2: <div id="num2" ng-model="num2">
<input type="number">
</div>
Multiplied: {{num1 * num2}}
<input type="submit" value="Submit">
</div>
</form>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.num1 ;
$scope.num2 ;
});
</script>
</body>
</html>
假设预设的PHP变量 num1 = 5
,如何将PHP变量 num1
传递给AngularJS应用?
How do I pass PHP variable num1
to AngularJS app, assuming preset PHP variable num1=5
?
推荐答案
将 ng-init ="num1('<?php echo $ num1?>')">
更改为 ng-init ="num1 ='<?php echo $ num1;?>'>
这篇关于将PHP变量传递给AngularJS变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!