我已经使用了 ng-include
如下所示。但它不起作用。你能告诉我为什么吗?提前致谢。
createOrEditPropertyModal.cshtml
<div ng-include="'propertymanagement/tabs/createPropertyForm.html'"></div>
我也试过这种方式。但是同样的404
错误。
<div ng-include="'tabs/createPropertyForm.html'"></div>
createPropertyForm.cshtml
<script type="text/ng-template" id="createPropertyForm.html">
<form name="createPropertyForm" role="form">
//removed for clarity
</form>
</script>
解决方案树
注意:当我把它放在同一个页面上时它就可以工作了。但是我怎样才能用上面的方式做到这一点呢?因为我想将该代码放在单独的 .cshtml
文件中。
<div ng-include="'createPropertyForm.html'"></div> //on same page where this works well
最佳答案
方法一:OP 的回答
你可以这样做。这是方法。你必须给出完整的路径,如下所示。
<div ng-include="'~/App/tenant/views/propertymanagement/tabs/createPropertyForm.cshtml'"></div>
如果您有任何性能问题,请在此处查看:ng-Include is very slow with the external template
方法二:
您不能使用 ng-include 包含 .cshtml 页面
为此你必须使用正确的 mvc 路由,比如
<div ng-include="'controller/Template/createPropertyForm'"></div>
这里的 createPropertyForm 就像是 action 方法的 id 部分
public ActionResult Template(string id)
{
switch (id.ToLower())
{
case "createPropertyForm":
return PartialView("~/Views/propertymanagement/tabs/createPropertyForm.cshtml");
default:
throw new Exception("template not known");
}
}
否则只需创建一个 html 页面并访问它
<div ng-include="'createPropertyForm.html'"></div>
关于javascript - Angular ng-include 不适用于 Razor 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591784/