5

我想在 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

看起来我无法将函数用于模板,而且看起来我无法访问属性或名称对象。

4

2 回答 2

1

您可以在函数中实现您的逻辑,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

于 2013-11-14T21:22:08.037 回答
1

看看这个: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>

它有效,但是对于您要解决的问题,这是一种非常糟糕的方法。

于 2013-11-14T22:46:13.263 回答