送的属性阵列指令 [英] send array of attributes to directive
本文介绍了送的属性阵列指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我读 NG-ATTR
指令,但我想给属性的阵列指令为例:
I read about ng-attr
for directive, but I want to send array of attributes to directive as example:
<myElement attributes="attributes"></myElement>
和我的指令为例:
myApp.directive('myElement', function(){
return {
restrict: 'E',
require: 'ngModel',
template: '<div>' +
'<!-- I want to add this attributes on the div element>' +
'</div>',
replace: true,
scope: {
attributes: '=attributes'
},
}
});
和属性是控制器为以下几点:
And the attributes is in controller as the following:
$scope.attributes = {"class": "test", "id": "test", "style": "color: 'red'"}
所以,我怎么能在指令设置在元素属性的阵列?
So, How can I set array of attributes in element in directive ?
推荐答案
在你的指令链接功能
link:function(scope, element, attrs){
var templateElement = angular.element(element[0].firstChild) //this is you template div el.
angular.forEach(scope.attributes, function(value, key){
//if you want to set the root element
attrs.$set(key, value)
//if you want to set template root div
templateElement.attr(key, value)
})
}
这篇关于送的属性阵列指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文