Javascript 如何通过从外部json文件angularjs获取路径来动态加载html模板

Javascript 如何通过从外部json文件angularjs获取路径来动态加载html模板,javascript,html,angularjs,json,Javascript,Html,Angularjs,Json,我面临加载包含一些描述的动态html模板的问题。我的基本要求是,当我点击特定的按钮时,它应该加载相应的html文件。通过从json文件获取路径。 我正在共享我的代码,我能够从json读取和显示url,但我无法显示该html页面的内容 #index.html <div> {{descriptionUrl}} <div ng-include="'./api-data/{{descriptionUrl}}'"></div> </div> {{d

我面临加载包含一些描述的动态html模板的问题。我的基本要求是,当我点击特定的按钮时,它应该加载相应的html文件。通过从json文件获取路径。 我正在共享我的代码,我能够从json读取和显示url,但我无法显示该html页面的内容

#index.html

 <div>
{{descriptionUrl}}

<div ng-include="'./api-data/{{descriptionUrl}}'"></div>

</div>

{{descriptionUrl}}
action.json
{
“产品”:[{
“descriptionUrl”:“view.html”
},
{
“descriptionUrl”:“add.html”
}
]
}
#view.html
看法
#add.html
添加

对于您的动态行为,请使用ng include with src,如下所示

 <ng-include src="{{your url}}"></ng-include>


引用:

首先,ng include根据当前范围计算表达式。所以,如果要使用固定路径,则需要使用引号使其成为字符串文字。第一个有效路径是

ng include=“'fixed_path'”

如果希望动态加载路径,可以创建如下路径

ng-include="'partial_path_1' + any_value_in_scope + 'again_fixed_path.html'"
或者,如果您想让代码更易于管理,您可以简单地

ng-include="returnPathFromScope()"
回答你的问题,

ng-include="'/api-data/' + descriptionUrl"

你面临的错误是什么?@parats:error我面临的是“这是正确的使用方法吗??mu descriptionUrl包含我在单击按钮时拥有的所有html路径,我想显示特定页面(.html)的内容,但{{url}}这是字符串格式的。请知道系统不会接受它。您可以使用字符串格式以及{}内的变量,因为您必须在“”中定义字符串。我很高兴它有帮助。@Paras:有没有办法写一个这样的函数并在html$scope.display=function()中调用它({var template=$templateCache.get(“./api data/”+$scope.descriptionUrl);}有。但它可能会打乱您的范围绑定。您可以使用ng bind html=“getTemplate()”。但不要忘记$sce.trustAsHtml(htmlContent):-不起作用。它甚至没有打电话。有些错误,请帮我解决
ng-include="'/api-data/' + descriptionUrl"