我一直在尝试将php变量的值传递给JavaScript.当js代码放入相同的php视图文件时,我可以这样做.但我不想把js代码放在php中,因为调试或进一步开发变得非常繁琐.
$this->registerJsFile('/js/myJs.js', [JqueryAsset::className()]);
Run Code Online (Sandbox Code Playgroud)
请帮忙.
对于从php到js的传递变量,你可以使用
Yii::$app->view->registerJs('var max = "'. $maxMinCost['max'].'"', \yii\web\View::POS_HEAD);
Run Code Online (Sandbox Code Playgroud)
小智 5
从 2.0.14 版本开始,有 registerJsVar() 可用。
$this->registerJsVar ('modalTitle', Yii::t('app', 'Update details' ), View::POS_BEGIN);
Run Code Online (Sandbox Code Playgroud)