- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到的情况是 Angular Controller 中的数据是从 CMS 填充的。其中一些数据包括可能包含重音字符作为 html 实体的 url(非英语、é á 等)然后将此 url 数据绑定(bind)到 anchor 标记的 href 属性。然而,我似乎无法在没有(我认为) Angular 转义html的情况下绑定(bind)数据。如果我使用 ng-init 设置相同的 url,则绑定(bind)工作正常。但这不是我的项目的选择。附加的示例 (& JSFiddle ) 说明了当绑定(bind)模型中的数据但 ng-init 中的值正确呈现时, Controller 在何处呈现无效链接(我假设是转义的结果)。
<body ng-app="myApp" ng-controller="myCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div>
<h1>Example from model json</h1>
<p>Link (href): <a href="{{item1.url}}">{{item1.url}}</a>
</p>
<p>Link (ng-href): <a ng-href="{{item1.url}}">{{item1.url}}</a>
</p>
</div>
<div>
<h1>Example from ng-init</h1>
<div ng-init="item2 = {'url':'http://www.example.com/file-áúó.pdf'}"></div>
<p>Link (href): <a href="{{item2.url}}">{{item2.url}}</a>
</p>
<p>Link (ng-href): <a ng-href="{{item2.url}}">{{item2.url}}</a>
</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope',
function($scope) {
$scope.item1 = {
'url': 'http://www.example.com/file-áúó.pdf'
};
}
]);
</script>
</body>
最佳答案
听起来您需要的是一种用纯 JavaScript 解码 HTML 实体的方法。使用 this 中提供的代码上一个问题与解析“&\amp;”有关作为起点,我做了一个 new fiddle这里展示了如何在不使用 ng-init 的情况下解码 HTML 实体。我还冒昧地删除了额外的代码,因为您没有进行任何编码(从封闭的对象中删除它会更麻烦,而不是值得,因为原始编码器使用该对象做的不仅仅是存储代码) 。另外,这里是示例格式的代码:
<body ng-app="myApp" ng-controller="myCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<div>
<h1>Example from model json</h1>
<p>Link (href): <a href="{{htmlEnDeCode.htmlDecode(item1.url)}}">{{htmlEnDeCode.htmlDecode(item1.url)}}</a>
</p>
<p>Link (ng-href): <a ng-href="{{htmlEnDeCode.htmlDecode(item1.url)}}">{{htmlEnDeCode.htmlDecode(item1.url)}}</a>
</p>
</div>
<div>
<h1>Example from ng-init</h1>
<div ng-init="item2 = {'url':'http://www.example.com/file-áúó.pdf'}"></div>
<p>Link (href): <a href="{{item2.url}}">{{item2.url}}</a>
</p>
<p>Link (ng-href): <a ng-href="{{item2.url}}">{{item2.url}}</a>
</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function($scope) {
$scope.item1 = {
'url': 'http://www.example.com/file-áúó.pdf'
};
// Credit to WaiKit Kung for SO answer at https://stackoverflow.com/a/20880789/4504895
$scope.htmlEnDeCode = (function() {
var charToEntityRegex,
entityToCharRegex,
charToEntity,
entityToChar;
function resetCharacterEntities() {
charToEntity = {};
entityToChar = {};
// add the default set
addCharacterEntities({
'&': '&',
'>': '>',
'<': '<',
'"': '"',
''': "'"
});
}
function addCharacterEntities(newEntities) {
var charKeys = [],
entityKeys = [],
key, echar;
for (key in newEntities) {
echar = newEntities[key];
entityToChar[key] = echar;
charToEntity[echar] = key;
charKeys.push(echar);
entityKeys.push(key);
}
charToEntityRegex = new RegExp('(' + charKeys.join('|') + ')', 'g');
entityToCharRegex = new RegExp('(' + entityKeys.join('|') + '|&#[0-9]{1,5};' + ')', 'g');
}
function htmlDecode(value) {
var htmlDecodeReplaceFn = function(match, capture) {
return (capture in entityToChar) ? entityToChar[capture] : String.fromCharCode(parseInt(capture.substr(2), 10));
};
return (!value) ? value : String(value).replace(entityToCharRegex, htmlDecodeReplaceFn);
}
resetCharacterEntities();
return {
htmlDecode: htmlDecode
};
})();
}]);
</script>
</body>
关于javascript - 如何将带有重音字符的 url 从 Angular Controller 输出到 href 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739295/
我正在尝试通过 node.js 中的 puppeteer 抓取数据 目前,我正在寻找一个脚本,用于抓取 well.ca 某个部分中的所有数据 现在,这是我试图通过 node.js 实现的方法/逻辑 1
href=""、href="#" 和 href="javascript:void(0)" 之间有什么区别? 它们有哪些不同的用途,什么时候一个比另一个更好? 最佳答案 href=""将重新加载当前页面
这是html代码: Delivery Schedule Route Abstract Report 我有 href 值。使用 href 值,我应该找到 anchor 标记并使用 jQuery
我不确定是不是因为我使用的是 Wordpress 但 this.href 没有返回包含它们的项目的 href(例如在“联系人”上它返回 http://www.domain.net/undefined反
这个问题在这里已经有了答案: Is there a "previous sibling" selector? (33 个答案) 关闭 8 年前。
这个问题在这里已经有了答案: Are you allowed to nest a link inside of a link? (9 个回答) 关闭 6 年前。 我有一个可点击的面板,其中有一个工具
我的 css 如下所示 ul.sometclass li a { display:inline-block; } 我的 html 看起来像 outer test
我没看明白这段代码是什么意思? a[href*=#]:not([href=#]) 谢谢! 最佳答案 简单地: a[href*=#] 获取 href 中包含 # 的所有 anchor (a)。 但是有:
document.getElementById("IDOFELEMENT"); 将其转换为链接的正确方法是什么? 我可以写吗 document.getElementById("IDOFELEME
所以我在我的 Next JS 应用程序中遇到了这个奇怪的问题,我导入了谷歌字体,如下所示 在我的浏览器中显示的不是 href,而是 data-href="...",所以问题是谷歌无法将此识别为链接
我想获取所选选项的 href 值,以便我现在可以转到使用按钮选择的链接。 这是我的代码
我正在尝试获取我的一个链接的 href 并将其克隆/复制到另一个链接的 href 这是我正在尝试的 var link = $('.topbook'); var link2 =
我基本上是试图从一个链接获取href,然后将其填充到另一个链接中: HTML: Link to thing Link to duplicate 脚本: $('.main-link').attr('hr
我使用的 CSS 工具提示必须包含在“a href”中才能工作。 iPad [add_to_cart_anchor item="ipad"]purchase the iPad[/add_to_c
我有一个以前是纯文本的电子邮件正文,但现在我把它变成了 HTML。电子邮件是使用多种方法生成的,但没有一种方法易于转换。 我有的是: Some content emailaddress@somethi
我正在尝试从网页中抓取数据,然后通过提取下一页的 href 来转到下一页。 但是,在这种情况下,包含下一页的 href 的标签是 href='#next'。使用 Chrome 检查此元素后,当我将鼠标
在我的 html 页面中,我看到一个链接,其“查看源代码”代码如下: 当我将鼠标悬停在链接上并单击它时,我看到了一个有效链接。但我无法找到生成此 URL 的位置和方式。我发现类 a.view 是在其
看完这篇文章net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/我
我想用 SvelteKit 构建一个 Web 应用程序,其中一页列出所有项目(带有潜在的搜索查询参数),然后每个单独的项目一页。如果我必须使用后端生成的所有内容以老式方式构建它,我的路径将是 /ite
此 js 搜索包含 page=fleet 的 href其中: var links = document.querySelectorAll('a[href*="page=fleet"]'); var h
我是一名优秀的程序员,十分优秀!