如何设置从AngularJS指令本地属性? [英] How to set a native attribute from AngularJS directive?

查看:152
本文介绍了如何设置从AngularJS指令本地属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写HTML类似于:

 < A HREF =sharedasset:img.png>测试与LT; / A>
&所述; IMG SRC =sharedasset:img.png/>

和有一个名为sharedasset指令,获取到 img.png 的完整路径,并设置该属性的值,而不具有什么属性的任何知识的指令名字是提前的时间。这可能吗?


解决方案

 <全路径=img.png>测试与LT; / A>
< IMG完整路径=img.png>

  app.directive('FULLPATH',函数(){
    返回{
        链接:功能(范围,元素,ATTRS){
            VAR fullPathUrl =HTTP://.../
            如果(元素[0] .tagName ===A){
                ATTRS $集('href属性,fullPathUrl + attrs.fullPath)。
            }其他{
                ATTRS $集('src'中,fullPathUrl + attrs.fullPath)。
            }
        },
    }
});

我不知道你在哪里越来越 fullPathUrl 的,所以我很难codeD它的连接功能。

I'd like to write HTML similar to:

<a href="sharedasset: img.png">test</a>
<img src="sharedasset: img.png"/>

And have a directive called "sharedasset" that gets the full path to img.png and sets the value of the attribute without the directive having any knowledge of what the attribute name is ahead of time. Is this possible?

解决方案

<a full-path="img.png">test</a>
<img full-path="img.png">

app.directive('fullPath', function() {
    return {
        link: function(scope, element, attrs) {
            var fullPathUrl = "http://.../";
            if(element[0].tagName === "A") {
                attrs.$set('href',fullPathUrl + attrs.fullPath);
            } else {
                attrs.$set('src',fullPathUrl + attrs.fullPath);
            }
        },
    }
});

I don't know where you are getting fullPathUrl from, so I hardcoded it in the link function.

这篇关于如何设置从AngularJS指令本地属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆