我正在尝试创建 aria-current
仅当 anchor href
时才在 anchor 中使用属性等于当前窗口路径。基本上,我想要aria-current="page"
如果有问题的 anchor 有 href
等于 View 路径中的窗口。我不想创建 aria-current
如果相关 anchor 没有 href
的属性等于 View 路径中的窗口。
有没有办法编写内联条件语句来创建或不创建 AngularJS (Angular1) 中的属性?
最佳答案
我认为没有“内联方式”可以做到这一点,但对于这种情况,您可以创建一个指令
这里有一个例子:
yourmodule.directive('myexampleblabla', function() {
return {
link: function(scope, elem, attr) {
if(elem.attr('href') === windows.location.href){
elem.attr('aria-current', true)
}
},
restrict: 'E',
};
});
HTML:
<a href="sample/path#a123" myexampleblabla>Your Anchor</a>
请注意:此代码未经测试,但应该能让您向前迈出几步。
对于您想要在 Angular 应用程序中修改 DOM 的每种情况,您都应该创建一个指令。在此处阅读有关指令的更多信息:
https://docs.angularjs.org/guide/directive
关于angularjs - 如何在 AngularJS 中有条件地创建属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47266811/