我没有时间为您编写指令,但您可以从ngSrc 指令源开始,仅在某些条件下设置属性...其中 90% 是注释和文档,只有一点代码在实际上是底部。我认为这是指令的基本链接功能,normalized
类似于ngSrc
,相同的代码用于ngHref
.
return {
priority: 99, // it needs to run after the attributes are interpolated
link: function(scope, element, attr) {
attr.$observe(normalized, function(value) {
if (!value)
return;
attr.$set(attrName, value);
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
// to set the property as well to achieve the desired effect.
// we use attr[attrName] value since $set can sanitize the url.
if (msie) element.prop(attrName, attr[attrName]);
});
}