AngularJS 承诺并在过滤器之上。无法在 'Window' 上执行 'atob'
AngularJS Promise & atop Filter. Failed to execute 'atob' on 'Window'
背景: 从事 Angular 前端工作。从后端检索 base64 编码的文档。 atob 给我一个错误,但一切正常。
怀疑: 我认为我的 atob 过滤器被调用了两次。在变量为 undefined/null 时命中 promise,然后在 promise 填充变量之后。
过滤代码:
angular.module('docFilters', []).filter('base64Decode', function() {
return function(cipherText) {
return atob(cipherText);
};
});
控制器代码:
angular.module('doc')
.controller('DocCtrl', ['$scope', 'DocService', function ($scope, DocService) {
$scope.doc = DocService.getCurrentDoc();
}]);
getCurrentDoc() 是一个 REST 请求。它向内部 Web 服务发出 GET 请求。
Html:
<span ng-bind-html="doc.content | base64Decode"></span>
这有效 'fine' - 如果不检查控制台,您永远不会知道。控制台显示:
"Error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
这对我来说是新的,所以我不确定是否有更好的方法。
atob(undefined); //throws an error
您需要修改过滤器
angular.module('docFilters', []).filter('base64Decode', function() {
return function(text) {
return text && atob(text);
};
});
为什么不让过滤器检查是否有值?
angular.module('docFilters', []).filter('base64Decode', function() {
return function(cipherText) {
if (cipherText) {
return atob(cipherText);
}
};
});
背景: 从事 Angular 前端工作。从后端检索 base64 编码的文档。 atob 给我一个错误,但一切正常。
怀疑: 我认为我的 atob 过滤器被调用了两次。在变量为 undefined/null 时命中 promise,然后在 promise 填充变量之后。
过滤代码:
angular.module('docFilters', []).filter('base64Decode', function() {
return function(cipherText) {
return atob(cipherText);
};
});
控制器代码:
angular.module('doc')
.controller('DocCtrl', ['$scope', 'DocService', function ($scope, DocService) {
$scope.doc = DocService.getCurrentDoc();
}]);
getCurrentDoc() 是一个 REST 请求。它向内部 Web 服务发出 GET 请求。
Html:
<span ng-bind-html="doc.content | base64Decode"></span>
这有效 'fine' - 如果不检查控制台,您永远不会知道。控制台显示:
"Error: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded."
这对我来说是新的,所以我不确定是否有更好的方法。
atob(undefined); //throws an error
您需要修改过滤器
angular.module('docFilters', []).filter('base64Decode', function() {
return function(text) {
return text && atob(text);
};
});
为什么不让过滤器检查是否有值?
angular.module('docFilters', []).filter('base64Decode', function() {
return function(cipherText) {
if (cipherText) {
return atob(cipherText);
}
};
});