Edu*_*nal 17 angularjs angularjs-directive
我有一个指令用一些常规HTML替换我的自定义标记.我想删除一些属性.例如,给定语法
<ui mybutton width="30"></mybutton>
Run Code Online (Sandbox Code Playgroud)
我的指令将其转换为
<div width="30" class="btn">bla bla </div>
Run Code Online (Sandbox Code Playgroud)
我想删除它"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);
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
我只是得到一个奇怪的循环(console.log显示几千次)
Lan*_*don 27
除非我缺少其他一些要求,否则你应该只能使用隔离范围和模板,如:
app.directive('backbutton',function(){
return {
restrict: 'A',
replace:true,
scope: {
x: '@',
y: '@'
},
template:'<button style="width: {{x}}px; height: {{y}}px">A back-button template</button>',
link: function (scope, element, attrs) {
element.removeAttr('x').removeAttr('y');
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43164 次 |
| 最近记录: |