在 ng-include 中使用变量作为表达式

Use variable as expression in ng-include

本文关键字:表达式 变量 ng-include      更新时间:2023-09-26

这段代码有效:

<div ng-include src="'Test.html'"></div>

此代码不会:

<div ng-include src="ctrl.URL"></div>

ctrl.URL 设置为 "Test.html" )。 我还将其设置为 'Test.html'"'Test.html'" 具有相同的结果。

如何成功地将其转换为表达式以在 ng-include src 中使用?我想我缺少一些关于如何解析字符串的知识,但我很确定'Test.html'应该工作。

我发现我太无能了,无法重命名自己的变量。网址真的是控制。URl.现在一切正常。

检查一下,也许这个小提琴会对你有所帮助。

网页和模板:

 <div ng-controller="Ctrl">
    <select ng-model="template" ng-options="t.name for t in templates">
     <option value="">(blank)</option>
    </select>
    url of the template: <tt>{{template.url}}</tt>
    <hr/>
    <div ng-include src="template.url" onload='myFunction()'></div>
  </div>
<!-- template1.html -->
<script type="text/ng-template" id="template1.html">
  Content of template1.html
</script>
<!-- template2.html -->
<script type="text/ng-template" id="template2.html">
    <p ng-class="color">Content of template2.html</p>
</script>

控制器:

function Ctrl($scope) {
    $scope.templates = [{
        name: 'template1.html',
        url: 'template1.html'},
    {
        name: 'template2.html',
        url: 'template2.html'}];
    $scope.template = $scope.templates[0];
    $scope.myFunction = function() {
        $scope.color = 'red';
    }
}