2016-03-15 60 views
0

我想下面的代码转换为指令:AngularJS指令接受参数

<object type="image/svg+xml" data="../images/icons/apple.svg" class="icon"></object> 

这样我就可以把它想:

<sicon type="apple"></sicon> 
<sicon type="banana"></sicon> 
<sicon type="orange"></sicon> 

我想:

.directive('sicon', function(){ 
    return{ 
     restrict: 'E', 
     scope:{ 
     type: '=type' 
     }, 
     template: '<object type="image/svg+xml" data="../images/icons/'+type+'.svg" class="icon"></object>' 
    }; 
    }) 

但并不工作

回答

2

您应该使用template函数中的指令。没有必要把价值孤立的范围内,因为它似乎是一个硬编码值

template: function(ele, attrs){ 
    return '<object type="image/svg+xml" data="../images/icons/'+attrs.type+'.svg" class="icon"></object>' 
} 

对于从孤立的范围动态值,它会寻找以下

template: '<object type="image/svg+xml" data="{{\'/images/icons/\'+type+\'.svg}}" class="icon"></object>'