Angular sce.trustAsUrl和javascript hrefs

Pau*_*one 5 angularjs

我正在尝试创建一个具有书签的页面,如下所示:

<a ng-href="{{getBookmarklet()}}">Bookmarklet</a>

function MyCtrl($scope) {
  $scope.getBookmarklet = function() {
     return 'javascript:alert(1)';
  }
}
Run Code Online (Sandbox Code Playgroud)

href被清理为不安全:javascript:alert(1).所以,我尝试过使用sce.trustAs来防止这种情况:

function MyCtrl($scope, $sce) {
  $scope.getBookmarklet = function() {
     return $sce.trustAsUrl('javascript:alert(1)');
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我仍然在我的网址上获得"不安全"前缀.我也尝试过trustAsJs,没有运气.我不想在我的应用程序中使用compileProvider将javascript:URL列入白名单,只需允许这一个实例.

Pro*_*Sim 0

我遇到了这个问题,并能够通过使用$sce.trustAsHtml()来解决它。

JS:

function MyCtrl($scope, $sce) {
  $scope.getBookmarklet = function() {
    return $sce.trustAsHtml('<a href="javascript:alert(1)">Bookmarklet</a>');
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<ng-bind-html ng-bind-html="::getBookmarklet()"></ng-bind-html>
Run Code Online (Sandbox Code Playgroud)

这可能会给 CSS 和其他指令带来问题,但这些问题通常可以解决。