简体   繁体   English

AngularJS:使用指令添加内联自定义代码

[英]AngularJS: Add inline custom code using a directive

Here's the scenario. 这是场景。

In the app, you can add inline custom code (HTML attributes ex. style="", onclick="alert('Test')") in an element (ex. input texts, divs). 在应用程序中,您可以在元素(例如输入文本,div)中添加内联自定义代码(HTML属性,例如style =“”,onclick =“ alert('Test')”)。 The custom code is binded to the main model and loaded to the element using a custom directive I've created. 自定义代码绑定到主模型,并使用我创建的自定义指令加载到元素。 I'm doing this to control dynamically generated fields that I want to hide and show based on different inputs. 我这样做是为了控制要根据不同输入隐藏和显示的动态生成的字段。

This is my custom directive that loads inline attributes on the element: 这是我的自定义指令,可在元素上加载内联属性:

app.directive('addCustomHtml', function() {
      return {
          scope: {
              customHtml: "="
          },
          link: function(scope, element, attributes){
              scope.$watch('customHtml', function(newVal, oldVal) {
                  if (newVal) {
                      var attrs = newVal.split('\n');
                      for (var i = 0; i < attrs.length; i++) {
                          var result = attrs[i].split('=');

                          var attr = result.splice(0,1);
                          attr.push(result.join('='));

                          if (attr[1]) {
                              element.attr(attr[0], attr[1].replace(/^"(.*)"$/, '$1'));
                          }
                      }
                  } else {
                      if (oldVal) {
                          var attrs = oldVal.split('\n');
                          for (var i = 0; i < attrs.length; i++) {
                              var attr = attrs[i].split('=');
                              if (attr[0]) {
                                  element.removeAttr(attr[0]);
                              }
                          }
                      }
                  }
              })
          }
      }
    });

It is binded to the element like this: 它被绑定到这样的元素:

<input type="checkbox" add-custom-html custom-html="checkbox1.customHtml">Yes

To see it in action, you can check the plunkr here: https://plnkr.co/edit/xjjMRPY3aE8IVLIeRZMp?p=preview 要查看它的实际效果,您可以在此处检查plunkr: https ://plnkr.co/edit/xjjMRPY3aE8IVLIeRZMp ? p = preview

Now my problem is, when I try to add AngularJS directives (ex. ng-show, ng-if) using my custom directive, AngularJS doesn't seem to recognize them and the model scope I'm passing inside. 现在我的问题是,当我尝试使用自定义指令添加AngularJS指令(例如ng-show,ng-if)时,AngularJS似乎无法识别它们以及我正在传递的模型范围。

Another problem is when I try to add vanilla Javascript event functions (ex. onclick="", onchange=""), it does work but sometimes AngularJS does not read them especially when the element has an ng-change, ng-click attributes. 另一个问题是,当我尝试添加普通的Javascript事件函数(例如onclick =“”,onchange =“”)时,它确实可以工作,但有时AngularJS不会读取它们,尤其是当元素具有ng-change,ng-click属性时。

Again, I am doing this approach on the app because I have generic fields and I want to control some of them by adding this so called "custom codes". 再次,我在应用程序上执行此方法,因为我具有通用字段,并且我想通过添加所谓的“自定义代码”来控制其中一些字段。

Any help would be highly appreciated!! 任何帮助将不胜感激!

If you want to add HTML code and compile it within current $scope , you should use the $compile service: 如果要添加HTML代码并在当前$scope对其进行编译,则应使用$ compile服务:

let someVar = $compile(yourHTML)($scope);
// you can now append someVar to any element and 
// angular specific markup will work as expected

Being a service, you'll need to inject it into current controller (or pre/post link function) to be able to use it. 作为一项服务,您需要将其注入到当前控制器(或前/后链接功能)中才能使用它。

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

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