【问题标题】:Dynamic created map triggering warning in angular动态创建的地图触发角度警告
【发布时间】:2017-08-22 04:26:13
【问题描述】:

在 Angular 中,我想根据输入的地址动态创建地图。我已经在 VueJS 中成功地做到了这一点。但从角度来看,这会触发安全警告。

HTML:

<iframe
    ng-src="https://maps.google.com/maps?&amp;q={{encodeURIComponent(item.address)}}&amp;output=embed"
    allowfullscreen>
</iframe>

我已尝试创建以下内容:

app.filter('trustAsResourceUrl', ['$sce', function ($sce) {
    return function (val) {
        return $sce.trustAsResourceUrl(val);
};

然后像这样管道它:

| trustAsResourceUrl}}

如果使用已经建立的 URL,但不是因为我试图从地址形成 URL。我得到以下信息:

错误:[$interpolate:noconcat] 插值时出错:严格 上下文转义不允许连接多个的插值 需要可信值时的表达式。看 http://docs.angularjs.org/api/ng.$sce

VueJS 非常简单,但我不能在这个项目中使用它。如果它给出任何想法,我会包括它:

methods: {
    getmap: function(){
        setTimeout(function () {
            const searchInput = document.getElementById('searchTextField');
            let addr = searchInput.value; 
            let embed = "<div class='form-group'><label for='exampleInputPassword1'>Map Preview</label><iframe frameborder = '0' scrolling= 'no' marginheight= '0' marginwidth= '0' src= 'https://maps.google.com/maps?&amp;q=" + encodeURIComponent(addr) + "&amp;output=embed' > </iframe></div>";

      $('.place').html(embed); }, 200)
    },

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    基本上你不能使用这个过滤器。您必须直接在作用域上创建要使用的字符串:

    this.src = $sce.trustAsResourceUrl("https://maps.google.com/maps?q="
       + encodeURIComponent(item.address) + "&output=embed");
    

    然后&lt;iframe ng-src="{{$ctrl.src}}"&gt;

    您必须更新整个src 而不仅仅是item.address

    【讨论】:

    • 这在技术上确实回答了我的问题,所以我会接受作为答案。谢谢!但是,在我的设置中实施看起来有点困难。有没有办法为该 URL 结构设置安全例外?如果是这样,我可以让它更简单。
    • 您可以参考docs.angularjs.org/api/ng.$sce的文档以尝试获得更多理解。不确定您是否有更好的选择。
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2018-02-20
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多