How to set a native attribute from AngularJS directive?

前端 未结 3 1319
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 03:39

I\'d like to write HTML similar to:

test

And ha

3条回答
  •  Happy的楠姐
    2020-12-30 04:33

    I didn't want the directive to care what the attribute name was, so this is what I ended up doing:

    test
    
    

    app.directive('sharedAsset', function (globalVars) {
        return {
            restrict: "A",
            scope: {
                attr: "@attr"
            },
            link: function (scope, element, attrs) {
                var fullPath = globalVars.staticWebsite + "/app/styles/main/" + attrs.sharedAsset + "?build=" + globalVars.buildNumber;
    
                attrs.$set(scope.attr || "src", fullPath);
            }
        };
    });
    

    Update: I changed it to default to the "src" attribute since images will be the most common scenario.

提交回复
热议问题