为什么当我尝试为iframe-src属性构建URL时,AngularJS会抛出错误
Why does AngularJS throw an error when I try to build a URL for an iframe src attribute?
我一直在修改AngularJS指令,我不明白为什么我试图用表达式构建iframe-src属性会引发错误。抛出的错误引用了以下URL,但恐怕我觉得它不是很有启发性:
http://errors.angularjs.org/1.3.14/$interpole/nooncat?p0=https%3A%2F%2Fwww.youtube.com%2Fembed%2F%7B%7BvideoId%7%7D
这到底意味着什么?我该怎么办?
以下是相关的JavaScript和HTML:
angular.module("myModule", [])
.directive("myDirective", function() {
return {
restrict: "EA",
scope: {
videoId: "@videoId",
width: "@width",
height: "@height"
},
// This seems to work as expected.
//template: '<iframe width="432" height="243" src="https://www.youtube.com/embed/8aGhZQkoFbQ" frameborder="0" allowfullscreen></iframe>',
// This seems to work as expected except for adding a little extra whitespace after the values.
//template: '<iframe width="{{width}}" height="{{height}}" src="https://www.youtube.com/embed/8aGhZQkoFbQ" frameborder="0" allowfullscreen></iframe>',
// This throws an error that refers to http://errors.angularjs.org/1.3.14/$interpolate/noconcat?p0=https%3A%2F%2Fwww.youtube.com%2Fembed%2F%7B%7BvideoId%7D%7D.
template: '<iframe width="432" height="243" src="https://www.youtube.com/embed/{{videoId}}" frameborder="0" allowfullscreen></iframe>',
replace: true
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<my-directive video-id="8aGhZQkoFbQ" width="432" height="243"></my-directive>
我感谢你的启迪。
我将保留以上内容以供参考,但这是一个使用控制器和$sce.trustAsResourceUrl的编辑版本,正如Muhammad Reda的回答中所建议的那样,以防它可以帮助任何人一起查看:
angular.module("myModule", ["ngSanitize"])
.directive("myDirective", function() {
return {
restrict: "EA",
scope: {
videoId: "@videoId",
width: "@width",
height: "@height"
},
template: '<iframe width="432" height="243" src="{{srcUrl}}" frameborder="0" allowfullscreen></iframe>',
replace: true,
controller: function($scope, $sce) {
$scope.srcUrl = $sce.trustAsResourceUrl("https://www.youtube.com/embed/" + $scope.videoId);
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-sanitize.min.js"></script>
<my-directive video-id="8aGhZQkoFbQ" width="432" height="243"></my-directive>
您的url需要包装在$sce.trustAsResourceUrl(YOUR_URL)
周围
文件。
在指令中添加controller
angular.module("myModule", ['ngSanitize'])
.directive("myDirective", function() {
.....
controller: function($scope, $sce) {
$scope.iframeUrl = 'https://www.youtube.com/embed/' + $scope.videoId;
$scope.iframeUrl = $sce.trustAsResourceUrl($scope.iframeUrl);
},
template: '<iframe src="{{ iframeUrl }}" frameborder="0" allowfullscreen></iframe>'
})
相关文章:
- AngularJS错误:提供程序必须从$get工厂方法返回值
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- AngularJS错误:$interpolate:$routeParams的interr插值错误
- AngularJS错误:请求的键的值不是对象.keys@[本地代码]
- AngularJS错误处理:根据错误数组显示错误
- AngularJs 错误:$http请求虽然在控制器中定义
- Angularjs 错误中的简单选项卡系统
- AngularJS错误:$injector:unp未知提供程序-$modalInstanceProvider
- AngularJS错误”;element.childs(..).slideToggle不是函数“;
- Promise创建自定义失败不是函数Angularjs错误
- AngularJS错误:$rootScope:infdig Infinite$digest循环:达到10次$digest
- AngularJS错误:$injector:modulerr我的浏览器窗口中出现模块错误
- 使用带有AngularJS错误的ngRoute
- Angularjs:错误:'追加'对未实现接口FormData的对象调用.jQuery.param/ad
- IE中的AngularJS错误与样式与ng样式
- AngularJS 1.3 - 错误:错误:modulerr 模块错误(使用 ng-view,$routeProvide
- AngularJS 错误 current.$$route 未定义
- AngularJS 错误:$rootScope:infdig 无限$digest循环
- AngularJS 错误:无法读取未定义的属性“get”
- AngularJS错误:“参数'FirstCtrl'不是一个函数,未定义”