【问题标题】:Fill the src attribute of ng-iclude with data from model用模型中的数据填充 ng-iclude 的 src 属性
【发布时间】:2016-02-28 08:38:47
【问题描述】:

为什么我会收到错误消息:

Error: [$interpolate:noconcat] Error while interpolating: '{{ content.src }}'
Strict Contextual Escaping disallows interpolations that concatenate multiple expressions when a trusted value is required.

使用这种语法:

<div id="content" class="content">
     <ng-include src="'{{ content.src }}'"/>
</div>

模型是

$scope.content = {};
$scope.content.src = "content.html";

【问题讨论】:

    标签: angularjs angularjs-ng-include


    【解决方案1】:

    我在我的一些项目中使用如下语法做到了这一点:

    <ng-include src="content.src"/>
    

    src 属性不需要括号符号,您可以只引用范围变量。仅当您使用字符串文字时才需要单引号。

    【讨论】:

    • 慢到什么程度?何时请求 html 片段?
    • 是的,这里是延迟,需要1秒以上。
    • 不要认为这与 ng-include 有任何关系,我到处使用它并且没有延迟,片段从服务器快速返回并且几乎立即呈现。似乎服务器可能存在问题,如果您在页面加载时查看 Chrome 开发工具中的网络选项卡,您可以看到每个 http 请求何时被触发,以及需要多长时间才能得到响应。
    【解决方案2】:

    试试这个方法:&lt;ng-include src="{{ content.src }}"/&gt;

    这样的事情也应该起作用: ng-include with a variable for src

    还没有测试,如果不行,请做一个plunker方便帮助

    编辑:

    &lt;ng-include src="content.src"/&gt; 是要走的路

    【讨论】:

    • 它也不起作用:Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{ content.src }}] starting at [{ content.src }}].
    • reddit.com/r/angularjs/comments/326det/… 根据这篇文章:删除 {{}} 标志。仍然无法测试
    • &lt;ng-include src="content.src"/&gt;helps,谢谢。您会更新您的答案,以便我将其标记为正确答案吗?
    • 别人更快.. :/
    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    相关资源
    最近更新 更多