gpt4 book ai didi

javascript - 将 PHP 变量传递给 AngularJS 变量

转载 作者:行者123 更新时间:2023-12-03 01:44:49 26 4
gpt4 key购买 nike

使用 AngularJS 数据绑定(bind),我想返回 num1 * num2 作为用户类型 num2num1 是一个 PHP 变量(用户无法更改)。以下,How do I pass PHP variables to angular js? ,我尝试过:

<!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 应用程序?

最佳答案

更改ng-init="num1('<?php echo $num1?>')"> , 至ng-init="num1 = '<?php echo $num1; ?>' ">

关于javascript - 将 PHP 变量传递给 AngularJS 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com