向 angularjs 指令添加和编译属性
Add and compile attributes to angularjs directive
我有一个指令<my-dir></my-dir>
,该指令应始终具有通过角度属性提供的特定功能:<my-dir ng-show="ctrl.shown"></my-dir>
.这里 ctrl 是通过指令定义controllerAs: 'ctrl'
定义的控制器。
我想通过在编译指令时自动添加属性来简化此指令并避免错误。这样其他用户只需键入 <my-dir>
.向指令添加属性很简单,但这些新属性似乎没有被编译。
app.directive("myDir", function() {
console.log('myDir');
return {
restrict: 'E',
controller: 'DirectiveController',
controllerAs: 'ctrl',
template: '<span>Controller Content</span>'
// Attempt to attach the 'ng-show' attribute to the element
link: function($scope, $element, attrs) {
$element.attr('ng-show', 'ctrl.shown');
}
};
});
我尝试了很多不同的事情:在link:
中添加属性,在compile:
中添加属性,甚至在添加属性后使用$compile($element[0]['my-dir'])($scope)
。
示例 plnkr:http://plnkr.co/edit/PREavAIn1vqUdZYLsypt?p=preview
使用ctrl.shown
的 plnkr 示例:http://plnkr.co/edit/dTWIzClZG4YpE76AMCWk?p=preview
更新
因此,将此逻辑放入指令的编译步骤似乎是正确的方向:
...
compile: function(tElem, tAttrs) {
// appears that you need to remove this attribute
// to stop the directive from looping infinitely
// through the compile step
tElem.removeAttr('my-dir');
tElem.attr('ng-hide', 'true');
return function(scope) {
$compile(tElem)(scope);
}
}
以下是更新的 plnkr: http://plnkr.co/edit/uyfJl8b1w5esYGv7SIsx?p=preview
看到这个问题
OP 的解决方案
根据Paul Trone对这个话题的回答,我想出了一个适合我的解决方案。
基本上,我在添加角度属性后重新编译元素。为了避免无限循环,我还使用自定义标记标记元素,并在标记元素时停止编译。
这样,我可以多次部署<my-dir></my-dir>
,每次在指令的根级别使用隐式自定义功能。这样就可以使用 ng-show 来隐藏整个指令,而不仅仅是其中的模板化元素。
angular.module('app', []);
angular.module('app').directive("myDir", function($compile) {
return {
restrict: 'E', // I prefer this directive to be Element-only
controller: 'DirectiveController',
controllerAs: 'ctrl',
template: '<span>Controller Content</span>',
/**
* Add the necessary angular attributes
* along with a 'compiled' attribute as tag
* such that the element doesn't infinitely compile itself
*
* This still allows multiple of the same directive to be
* compiled at the same time
*/
compile: function(tElem, tAttrs){
// Check if the element has been tagged
if(! tAttrs['compiled']) {
// Tag the element so it doesn't compile again
tElem.attr('compiled', true)
// Add custom angular attributes
tElem.attr('ng-show', 'ctrl.shown');
// Recompile
return function(scope) { $compile(tElem)(scope); };
}
}
};
});
angular.module('app').controller("DirectiveController", function() {
var shown = false;
});
例 : http://plnkr.co/edit/wbs0vcFmz15oL2twm6AC?p=preview
或者,可以将观察程序附加到一个变量,该变量将类模拟切换到ng-show的工作方式,但这将开始复制ng-show属性预期的确切功能。
不确定我是否理解您要正确执行的操作,但是您为什么不像这样将属性添加到模板中呢?
template: '<span ng-show="ctrl.shown">Controller Content</span>'
- AngularJS指令出错-无法读取属性'编译'的未定义
- 无法读取属性'编译'的未定义.可以'我似乎没有发现错误
- 在JavaScript中编译搜索后,获取链接到函数的属性标记的Element
- Angular 指令内部 HTML 重新编译,动态创建绑定属性
- AngularJS - 是否可以在链接或编译中的指令上更改ngModel属性的值
- 向 angularjs 指令添加和编译属性
- Bootstrap 不会编译 css 语法错误:无法读取属性 'toCSS'
- 将v-attr与包含冒号的属性一起使用'xmlns:href'不编译
- 在没有$scope更改的情况下强制重新编译已筛选的ng属性
- 在闭包高级编译中获取对象的属性名称
- 闭包编译器未重命名属性和方法(高级编译)
- 从另一个指令动态添加、编译和链接ng类属性
- AngularJS中多属性指令的编译和链接顺序
- Angular 2的属性注释无法编译
- 如何创建一个新指令来编译Angularjs中的其他指令属性
- Angular 2 + typescript在编译时找不到属性
- 用函数表达式命名对象属性似乎是不可能的.但是它可以编译
- LESS(客户端)不编译某些CSS属性
- Babel不会编译ES6计算的属性名
- 闭包编译器重命名问题,由于名称属性