我有一个指令,用一些常规的HTML替换我的自定义标签。 我想删除一些属性。例如,给定的语法AngularJS删除属性
<ui mybutton width="30"></mybutton>
我的指令将其转换为
<div width="30" class="btn">bla bla </div>
我想删除"width=30"
并添加style="width:{{old width value here}}"
我一直在编译和链接功能试验。我应该在编译还是链接功能中做到这一点?
我以为我不得不在编译函数中这样做,因为我想在模板中进行修改。
看到它住在http://jsfiddle.net/WptGC/2/警告:您的浏览器可能会挂起! 实时安全地看到它http://jsfiddle.net/WptGC/3/让一切崩溃的代码都被评论了。
.directive('mybutton', function($compile) {
return {
restrict: 'A',
//transclude: true,
template: '<div class="this is my subscreen div" style="width:{{width}}"></div>',
replace: false,
/*scope: {
width: '@',
height: '@',
x: '@',
y: '@'
},*/
compile: function($tElement, $tAttrs) {
console.log("subscreen template attrs:");
console.log($tAttrs);
var el = $tElement[0];
//el.getAttribute('width');
var stylewidth = el.getAttribute('width');
el.removeAttribute('width');
return function(scope) {
$compile(el)(scope);
}
}
}
})
我只是得到一个奇怪的循环(即执行console.log显示了几千次)
的原因,你都拿到了环是你调用$编译相同的元素,因此编译函数被再次呼吁。 – kvetis 2015-01-28 13:31:02