【发布时间】:2015-04-02 00:47:21
【问题描述】:
我确定以前有人问过这个问题,但是很难提出问题,因此也很难搜索...
我的一个应用程序使用了几个第三方指令之一。目前,我正在处理的指令是 xeditable 库中的指令,但这个问题不仅仅是关于那个库,因为我之前遇到过这种需求。
目前,我的代码如下所示:
<div ng-switch on="editTypeNeeded">
<div ng-switch-when="textarea" editable-textarea="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
{{myvar}}
...
and other stuff
</div>
<div ng-switch-when="textfield" editable-text="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
{{myvar}}
...
and other stuff
</div>
<div ng-switch-when="checkbox" editable-checkbox="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
{{myvar}}
...
and other stuff
</div>
<div ng-switch-when="date" editable-bsdate="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
{{myvar}}
...
and other stuff
</div>
...
etc...
</div>
当每个switch 选项之间发生变化的唯一是“editable-textarea”、“editable-text”等时,这是大量的代码重复。
我想要的是将以上所有内容替换为以下内容:
<div {{directiveName}}="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
{{myvar}}
...
and other stuff
</div>
这显然是行不通的,但必须有一种方法来做这样的事情......
更新:
我最终使用了选定的答案,但稍作修改以允许变量指令名称和指令值。像这样:
.directive('useDirective', function ($compile) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
attrs.$observe('useDirective', function(directiveNameAndValue) {
if (directiveNameAndValue) {
var nameAndValue = directiveNameAndValue.split('='),
dirName = nameAndValue[0],
value = nameAndValue[1] || '';
elem.attr(dirName, value);
elem.removeAttr('use-directive');
$compile(elem)(scope);
}
});
}
};
});
我是这样使用它的:
<div use-directive="{{getDirectiveType(value)}}=value" class="pointer" onbeforesave="notifyOfChange($data)">{{value}}</div>
Plunker 示例:http://plnkr.co/edit/JK5TkFKA2HutGJOrMo8a?p=preview
更新 2:
我发现上述技术似乎不能很好地与 angular-animate.js 库配合使用。它会导致该库多次引发以下错误:
TypeError: Cannot read property 'parentNode' of undefined
at angular-animate.js:1203
at angular-animate.js:539
at n.$digest (angular.js:14358)
at n.$apply (angular.js:14571)
at angular.js:16308
at e (angular.js:4924)
at angular.js:5312
不过,页面似乎仍然可以正常工作。
【问题讨论】:
-
编写自己的指令来体现上述所有逻辑?
-
我不明白为什么您不能将该开关逻辑粘贴到指令的模板中,然后在指令上为 editTypeNeed 传递或设置一个属性。将所有 HTML 保留在指令中,并在表单中使用指令来保持整洁。
标签: angularjs