Angular.js aHrefSanitizationWhitelist 不工作?

Angular.js aHrefSanitizationWhitelist not working?

我正在对 basic Angular.js tutorial 进行一些试验,但我在尝试设置 url-schema 的白名单时遇到了问题。

基本上我正在尝试将自定义方案 (cust-scheme) 添加到 angular 的白名单中,以避免它作为前缀 [=41] =]s 与 unsafe:.

根据this Whosebug的回答,我只需要添加$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|cust-scheme):/); 应用程序的配置参数。

我尝试了以下方法:

'use strict';

/* App Module */

var phonecatApp = angular.module('phonecatApp', [
  'ngRoute',
  'ngSanitize',
  'phonecatAnimations',

  'phonecatControllers',
  'phonecatFilters',
  'phonecatServices'
]);

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }],
  ['$compileProvider',
  function( $compileProvider ) {   
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|cust-scheme):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|cust-scheme):/);
  }
]);

但它不起作用。路由很好,但 cust-scheme 架构未列入白名单。 有什么我想念的吗?也许我做错了多重配置?

我还尝试了以下方法:

'use strict';

/* App Module */

var phonecatApp = angular.module('phonecatApp', [
  'ngRoute',
  'ngSanitize',
  'phonecatAnimations',

  'phonecatControllers',
  'phonecatFilters',
  'phonecatServices'
]);

phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }]
);

phonecatApp.config(function( $compileProvider ) {   
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|cust-scheme):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|cust-scheme):/);
  }
);

在第二种情况下,架构已列入白名单,但路由不再起作用。

感谢任何帮助!

您真诚的, Angular.js 新手 :)

看起来您在语法上有点偏差,所以让我们尝试 "merge" 将这两个代码片段放在一起。希望这段代码是自我描述的,你可以看到我们正在以最小安全和冗长的方式将 $routeProvider$compileProvider 一起注入,以立即调用我们的应用程序 .config

phonecatApp.config(['$routeProvider', '$compileProvider', function($routeProvider, $compileProvider) {

    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension):/);
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension):/);

    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
}]);