在文件中处理 Javascript 变量.aspx有条件地显示元素



有这个.html片段:

<body>
<div id="testDiv">
This always appears
</div>
<%
if(step==1)
{%>
html code
<% } 
if (step==2) 
{
%>
other html code
<% } %>
</body>

如何访问使用script标签导入的.js文件中存在的变量"step",以便有条件地显示 HTML 元素? 我正在尝试以某种方式模拟AspX中Angular2的*ngIf功能。 谢谢!

你可以像这样使用它并创建一个javascript变量。

<script type="text/javascript">
var step = <%= step %>;
alert(step);
</script>

您也可以使用脚本管理器

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setValue", "var step = " + step + ";", true);

在这两种情况下,step都可以在.js文件中使用。

最新更新