簡體   English   中英

帶有angularjs指令的動態標簽

[英]Dynamic tag with angularjs directive

我想在angularjs中創建一個元素指令,它從作為屬性傳遞的json blob生成一個html元素。 我嘗試了以下幾種變體......

demoApp.directive("element", function() {
    return {
        restrict: "E",
        scope: {
            attributes: "@",
            name: "@"
        },
        template:         
            function(name, attributes) {
                var templateString = "<" + attributes.tag;
                for (attribute in attributes) {
                    if (attribute != "name_displayed" && attribute != "tag") {
                        templateString += " " + attribute + "=\"" attributes[attribute] + "\"";
                    }
                }
                templateString += " name=\"" field + "\"";
                templateString += ">";
                templateString += "</" + attributes.tag + ">";
                return attributes.name_displayed + ": " + templateString;
            }(name, attributes)
    };
});

html看起來像

<div ng-repeat="(name, attributes) in fields">
    <element name="{[{name}]}" attributes="{[{attributes}]}"></element>
</div>

json對象的屬性是什么樣的

{"name_displayed":"Agency","size":"30","tag":"input","type":"text"}

一個名字看起來像

agency

看起來我不能將函數用於模板,看起來我也無法訪問屬性或名稱對象。

您可以在link功能而不是template實現邏輯。 嘗試這個:

HTML

<element ng-repeat="field in fields" />

JavaScript的

angular.module('demo', []).
    controller('demoCtrl', ['$scope', function($scope) {
      $scope.fields = [{
        "tag": "input",
        "type": "text",
        "value": "Demo app",
        "name": "my_input",
        "label": "My Text"
      }, {
        "tag": "input",
        "type": "checkbox",
        "checked": "checked",
        "name": "my_checkbox",
        "label": "My Checkbox"
      }, {
        "tag": "input",
        "type": "button",
        "value": "Click Me",
        "name": "my_button"
      }];
    }]).
    directive('element', function() {
      return {
        restrict: "E",
        replace: true,
        template: "<div></div>",
        link: function(scope, element, attrs) {
          var label,
              el,
              key,
              field;

          field = scope.field;

          if('label' in field) {
            label = document.createElement('label');
            label.innerHTML = field.label;
            element.append(label);
            element.append(document.createTextNode(': '));
          }

          el = document.createElement(field.tag);
          for(key in field) {
            if(field.hasOwnProperty(key) && // avoid prototype properties
                key !== 'tag' && // avoid tag
                key !== 'label' && // avoid label
                key[0] !== '$' // avoid angular staff derived from scope
            ) {
              el.setAttribute(key, field[key]);
            }
          }
          element.append(el);
        }
      };
    });

這是一個工作示例: http//plnkr.co/edit/B1RigXrzA2l1kIVNVXGw?p = preview

看看這個: http//jsfiddle.net/es4Y6/1/

var app = angular.module('hmm', []);

function ctrl($scope) {
    $scope.fields = {
        first: '{"name_displayed": "Agency", "size": "30", "tag": "input", "type": "text"}',
        second: '{"name_displayed": "Foo", "size": "30", "tag": "input", "type": "password"}',
        third: '{"name_displayed": "Bar", "size": "30", "tag": "input", "type": "number"}'
    };
}

app.directive('blah', function() {

    var template = function(name, attributes) {
        var templateString = "<" + attributes.tag;
        for (var attribute in attributes) {
            if (attribute != "name_displayed" && attribute != "tag") {
                templateString += " " + attribute + '="' + attributes[attribute] + '"';
            }
        }
        templateString += ' name="' + name + '"';
        templateString += ">";
        templateString += "</" + attributes.tag + ">";
        return attributes.name_displayed + ": " + templateString;
    };

    return {
        restrict: "E",
        link: function(scope, element, attrs){
            var attributes = angular.fromJson(attrs.attributes);
            var tpl = template(attrs.name, attributes);
            element.html(tpl);
        }
    };

});

我假設“json blob”是指json字符串。 如果沒有,那么你的意思是JS對象。 在這種情況下,更新$scope.fields並刪除angular.fromJson()

<div ng-app="hmm">
    <div ng-controller="ctrl">
        <div ng-repeat="(name, attributes) in fields">
            <blah name="{{name}}" attributes="{{attributes}}"></blah>
        </div>
    </div>    
</div>

它有效,但是對於你想要解決的問題來說,這是一個非常糟糕的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM