无法在指令中添加角属性 [英] Not able to add attribute in directive in angular
本文介绍了无法在指令中添加角属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的code在我的指令
I have this code in my directive
compile: function compile (tElement, tAttributes, transcludeFn) {
if (tAttributes.drag == 'false') {
tElement.find('.myclass').removeAttr('draggable');
}
//attrs.$set('ngModel', 'new value');
return {
pre: function preLink (scope, element, attributes, controller, transcludeFn) {
// Pre-link code goes here
},
post: function postLink (scope, element, attributes, controller, transcludeFn) {
这正常工作
但我想基于这样的布尔添加属性,而不是删除属性。
But i want to add attribute instead of remove attribute based on boolean like this
if (tAttributes.drag == 'true') {
tElement.find('.myclass').attr('draggable');
}
但是,这是行不通的。
But this is not working.
我的事情,我需要添加后重新编译元素,但我不知道该怎么做。
I thing i need to recompile element after adding but i don't know how to do it
推荐答案
尝试添加该指令定义的模板函数的属性。
Try adding the attribute in template function of the directive definition.
module.run(function ($templateCache, $http) {
$http.get('__templateURL__')
.then(function (response){
$templateCache.put('__templateID', response.data)
})
});
module.directive('x', function ($templateCache) {
return {
template: function (tEl, tAttrs) {
var template = $($templateCache.get('__templateID'));
if (tAttrs.drag == 'true') {
template.find('.myclass').attr('draggable');
}
return template[0].outerHTML;
}
}
});
这篇关于无法在指令中添加角属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文