简体   繁体   English

角度的变量指令名称

[英]Variable directive name in angular

I'm sure this has been asked before, but it's kind of hard to put into a question and, therefore, is also hard to search for... 我敢肯定,这已经被问过了,但是很难提一个问题,因此,也很难寻找...

A piece of my app uses one of several third-party directives. 我的一个应用程序使用了多个第三方指令之一。 The directives I'm messing with, at the moment, are those in the xeditable lib, but this question isn't just about that lib, as I've come across this need before. 目前,我正在使用的指令是xeditable lib中的指令 ,但是这个问题不仅仅与那个lib有关,因为我之前遇到过这种需求。

My code, currently, looks something like the following: 目前,我的代码如下所示:

<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>

That's a TON of code duplication when the only thing that changes between each switch option is "editable-textarea", "editable-text", etc. 当每个switch选项之间唯一改变的是“ editable-textarea”,“ editable-text”等时,这就是大量的代码重复。

What I would like is to replace all of the above with something like this: 我想要的是将所有上述内容替换为以下内容:

<div {{directiveName}}="myVar" onbeforesave="doBeforeSaveStuff($data)" class="whatever" ng-class="whatever2()" ng-mouseover="doStuff()" some-other-directive="doMoreStuff()">
    {{myvar}}
    ...
    and other stuff
</div>

which, obviously, doesn't work, but there's got to be a way to do something like that... 显然,这是行不通的,但是必须有一种方法可以做类似的事情...


Update: 更新:

I ended up using the selected answer, but with a slight modification to allow for both a variable directive name AND a directive value. 我最终使用了选择的答案,但稍作修改,以允许同时使用变量指令名称和指令值。 Like so: 像这样:

.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);
                }
            });
        }
    };
});

And I use it like this: 我这样使用它:

<div use-directive="{{getDirectiveType(value)}}=value" class="pointer" onbeforesave="notifyOfChange($data)">{{value}}</div>

Plunker example: http://plnkr.co/edit/JK5TkFKA2HutGJOrMo8a?p=preview 柱塞示例: http ://plnkr.co/edit/JK5TkFKA2HutGJOrMo8a?p=preview


Update 2: 更新2:
I've found that the above technique doesn't seem to play nicely with the angular-animate.js lib. 我发现上述技术似乎无法与angular-animate.js库配合使用。 It causes that lib to kick up the following error a bunch of times: 它导致该lib多次引发以下错误:

 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

though, the page still seems to work fine. 不过,该页面似乎仍然可以正常工作。

You can create a directive to-rule-them-all: 您可以为所有规则创建一条指令:

<div lord-of-the-directives="{{directiveName}}">

Which would be like 哪个会像

module.directive('lordOfTheDirectives', function ($compile) {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
      attrs.$observe('lordOfTheDirectives', function(dirName) {
        if (dirName) {
          elem.append('<div ' + dirName + '>');
          $compile(elem)(scope);
        }
      });
    }
  };
});

(not tested but that's the idea) (未经测试,但这就是想法)

Update by request in comment, to keep the element as is (with its classes and stuff): 通过在注释中的请求进行更新 ,以使元素保持不变(及其类和内容):

module.directive('lordOfTheDirectives', function ($compile) {
  return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
      attrs.$observe('lordOfTheDirectives', function (dirName) {
        if (dirName) {
          elem.attr(dirName, '');
          elem.removeAttr('lord-of-the-directives');
          $compile(elem)(scope);
        }
      });
    }
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM