我创建了一个自定义指令来创建动态UI元素(具有相应 Headers 的Checkbox)以显示基于JSON数据的书籍 . 我的目的是在单击复选框(特定书籍)上创建文本框,基于onSelection数组(总份数和已售出副本)的可用性,onSelection的 Headers 仅作为所单击书籍的文本框内的占位符 . 到目前为止,我已经完成了显示文本框而没有单击复选框 . 但是,只有当选中的书中的onSelcetion数组与特定书籍相关时,我才需要显示文本框 . 任何帮助将非常感激 .

在plunker中包含整个代码Code in plunker

HTML

<!DOCTYPE html>
<html ng-app="myApppli">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
</head>

<body>
<div ng-controller="mycontroller as ctrll">
    <content-item ng-repeat="item in ctrll.content" content="item">
    </content-item>

<div style ="margin-top: 30px;" ng-repeat="item in ctrll.content" content="item" ng-if ="item.onSelection">
<div ng-repeat="onSelection in item.onSelection">

<text-field-new></text-field-new> 
</div>  </div>
</div>

</body>

</html>

app.js

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

app.config(function ($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist(['self', '**']);
});

app.constant('URL', '');


app.factory('MedService', function ($http, URL) {
var getMeds = function () {
    return $http.get(URL + 'books.json');
};

return {
    getMeds: getMeds
};
});

app.factory('TemplateService', function ($http, URL) {
var getTemplates = function () {
    return $http.get(URL + 'templates.json');
};

return {
    getTemplates: getTemplates
};
});
datalist = [];
app.controller('mycontroller', function (MedService) {
var ctrll = this;

ctrll.content = [];

ctrll.fetchContent = function () {
    MedService.getMeds().then(function (result) {
        ctrll.content = result.data.books.contents;
                        datalist = ctrll.content;
    });
};
ctrll.fetchContent();
});
app.directive('contentItem', function ($compile, TemplateService) {
var getTemplate = function (templates, type) {
    var types = type.type;
    var template = '';            

    switch (types) {
        case 'textbox':
            template = templates.textboxTemplate;
            break;
        case 'CHECKBOX':
            template = templates.checkboxTemplate;
            break;
    }

    return template;
};

var linker = function (scope, element, attrs) {

    TemplateService.getTemplates().then(function (response) {
        var templates = response.data;

        element.html(getTemplate(templates, scope.content));

        $compile(element.contents())(scope);


    });
};

return {
    restrict: 'E',
    link: linker,
    scope: {
        content: '='
    }
};
});
app.directive("textFieldNew", ['$rootScope', function($rootScope) {
    return {
        restrict : 'E',
        templateUrl : 'text-field-new.html',
        scope : {
         field : "="
        },



        link : function (scope, element, attr) {
            scope.interfaceLabels = scope.$parent.interfaceLabels;
            scope.emailTextfieldClearButton = $rootScope.emailTextfieldClearButton;
            scope.enabledCursor = $rootScope.enabledCursor;

            //TODO - text field validation methods
            //Update keyboard and higiTextField to have field validation be triggered in higiTextField instead of keyboard.js
            //scope.onChangeMethod = scope.$watch('field.text', 
function(newVal, oldVal){
            //     if(typeof(scope.field.callback) === "function"){
            //    scope.field.callback();
            //}
            //});

            scope.setIndex= function(id){
                $rootScope.textFieldIndex = document.getElementById(id).selectionStart;
            };
            scope.startDrag = function(e){
                var e = window.event;
                scope.startDragX = e.pageX;
                e = null;
            };
            scope.clearSelected = function(id){
                var e = window.event;
                scope.stopDragX = e.pageX;
                if(scope.stopDragX > scope.startDragX){
                    document.getElementById(id).setSelectionRange(document.getElementById(id).selectionEnd, document.getElementById(id).selectionEnd);
                    $rootScope.textFieldIndex = document.getElementById(id).selectionEnd;
                }else {
                    document.getElementById(id).setSelectionRange(document.getElementById(id).selectionStart, document.getElementById(id).selectionStart);
                    $rootScope.textFieldIndex = document.getElementById(id).selectionStart;
                }
                e = null;
            };
            scope.clearTextField = function(){
                $rootScope.targetField.text ="";
                $rootScope.hideEmailExtensionTop();
                document.getElementById($rootScope.targetField.id).value = $rootScope.targetField.text;
                if(typeof(scope.field.callback) === "function"){
                    scope.field.callback();
                }
            };

            //Update labels if language is changed.
            scope.enabledCursorWatch = $rootScope.$watch('enabledCursor', function(){
                scope.enabledCursor = $rootScope.enabledCursor;
            });


        }
    }
  }]);