javascript - Angular IFrame ng-src : Interpolate Error

标签 javascript angularjs iframe

我正在尝试使用以下方法将 Google map 嵌入到 iframe 中:

<div ng-repeat="event in ctrl.events">
    <iframe src="{{'https://www.google.com/maps/embed/v1/place?q=(' + event.location.latitude + '%2C' + event.location.longitude + ')&zoom=12&key=API_KEY'}}">
    </iframe>
</div>

但是我得到:

Error: $interpolate:interr Interpolation Error

我的数据集是这样的

$scope.events = 
[
    { 
        location : { 
            longitude: 1.1,
            latitude: -1.1
        }
    }
]

我做错了什么?我试过 ng-src 但我得到了同样的错误

最佳答案

您必须像这样在您的应用配置中允许 map URL:

myApp.config(["$sceDelegateProvider", function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist([
        // Allow same origin resource loads.
        "self",
        // Allow loading from Google maps
        "https://www.google.com/maps/embed/v1/place**"
    ]);
}]);

来自docs :

The $sceDelegateProvider provider allows developers to configure the $sceDelegate service. This allows one to get/set the whitelists and blacklists used to ensure that the URLs used for sourcing Angular templates are safe. Refer $sceDelegateProvider.resourceUrlWhitelist and $sceDelegateProvider.resourceUrlBlacklist

查看下面的工作示例

(当然key错了所以Map会报错)

var myApp = angular.module("sa", []);

myApp.config(["$sceDelegateProvider",
  function($sceDelegateProvider) {
    $sceDelegateProvider.resourceUrlWhitelist(["self",
      "https://www.google.com/maps/embed/v1/place**"
    ]);
  }
]);

myApp.controller("foo", function($scope) {
  $scope.events = [{
    location: {
      longitude: 1.1,
      latitude: -1.1
    }
  }]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="sa" ng-controller="foo">
  <div ng-repeat="event in events">
    <iframe src="{{'https://www.google.com/maps/embed/v1/place?q=(' + event.location.latitude + '%2C' + event.location.longitude + ')&zoom=12&key=API_KEY'}}">
    </iframe>
  </div>
</div>

关于javascript - Angular IFrame ng-src : Interpolate Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35848727/

相关文章:

javascript - 如何保存从网页间接加载的图像

javascript - 将 C 编译为 wasm,使用与 WasmFiddle 相同的设置

javascript - 如何使用 cordova inappbrowser 在 Ionic/Angular 混合应用程序中打开链接

javascript - iOS 上的 IFrame 高度问题(移动版 safari)

javascript - 使用javascript设置IFrame allowfullscreen

javascript - 对于 JavaScript 原型(prototype)继承,为什么不能在子构造函数中创建并保存父对象?

javascript - Vue3 : injection "Symbol(pinia)" not found

html - 覆盖第三方输入中的占位符值

javascript - 在 AngularJS 中通过状态链路由的正确方法是什么?

javascript - 在 iFrame 中加载外部跨域 HTML 内容还是从异步 JavaScript 加载?