2016-08-19 1 views
1

동적으로 요소를 추가하고 제거하는 데 사용되는 지시문을 만듭니다. 하지만 요소를 추가하거나 제거하는 동안 ng-click을 여러 번 실행합니다. 제안을하십시오.지시문에서 ng-click을 여러 번 실행합니다. angularjs

여기서 값 사용은 1 => 텍스트 상자를 선택하는 것을 의미합니다. 2 => 선택 버튼; 3 => 라디오 버튼; 4 => CheckBox의 5 => +/-

HTML :

<body ng-app = "mainApp" ng-controller = "loginController"> 
<div id="sampleeid"> 
    <incrementrowsdirective idvalue="sto" 
     arrayvalues='[]' 
     labelvalues='["Concepto","Monto","Add/Remove"]' 
     enablevalues='[2,1,5]'></incrementrowsdirective> 
    <button ng-click="SampleButton()">Sample 
     Button</button> 
</div> 

CODE :

gasapp.directive("incrementrowsdirective", function ($compile) { 
    var trheaderelements; 
    var tablenm; 
    var tablenm1; 
    var trelements; 
    var trfirstelement; 
    var firstelement; 

    return { 
     restrict: 'AE', 
     scope: { 
      idvalue: '@' 
     }, 
     template: function (elem, attr) { 
      return '<div class="col-md-8 col-sm-11 col-xs-11"><table id="tableheaderid"></table> <table id="tableid' + attr.idvalue + '" class="table table-condensed table-striped table-bordered table-hover no-margin">' + 
       '</table></div>' 
     }, 

     link: function (scope, iElement, iAttrs) { 
      scope.labelval = JSON.parse(iAttrs.labelvalues); 
      scope.enableval = JSON.parse(iAttrs.enablevalues); 
      scope.arrt = JSON.parse(iAttrs.arrayvalues); 
      scope.c = 0; 
      scope.count = 1; 

      trheaderelements = '<thead><tr id="mytrheader' + scope.idvalue + '" >'; 
      angular.forEach(scope.labelval, function (value, key) { 
       trheaderelements = trheaderelements + '<th style="width: 20%;">&nbsp;' + value + ' </th>'; 
      }); 

      trheaderelements = trheaderelements + "</tr></thead>"; 

      tablenm = angular.element(document.getElementById('tableid' + scope.idvalue)).append(trheaderelements); 
      $compile(tablenm)(scope); 


      trfirstelement = "<tr id='mytrfirstelement'>"; 


      angular.forEach(scope.enableval, function (value, key) { 
       if (value == 1) { 
        trfirstelement = trfirstelement + '<td><input type="text" name="text1name' + scope.idvalue + value + key + '" ng-model="text1value' + scope.idvalue + value + key + '" id="text1valueeid' + scope.idvalue + value + key + '" > </td>'; 
       } 
       if (value == 2) { 
        trfirstelement = trfirstelement + '<td><select id="selectid1value' + scope.idvalue + value + key + '" name="selectname1val' + scope.idvalue + value + key + '" ng-model="selectmodel1val' + scope.idvalue + value + key + '" style="width:185px;"><option>..select..</option><option>Brother</option><option>Sister</option><option>Wife/Husband</option></select></td>'; 
       } 
       if (value == 3) { 
        trfirstelement = trfirstelement + '<td><input type="radio" id="radio1idvalue' + scope.idvalue + value + key + '" name="radio1nameval' + scope.idvalue + value + key + '" ng-model="radio1modelval' + scope.idvalue + value + key + '" > </td>'; 
       } 
       if (value == 4) { 
        trfirstelement = trfirstelement + '<td><input type="checkbox" id="checkbox1idval' + scope.idvalue + value + key + '" name="checkbox1nameval' + scope.idvalue + value + key + '" ng-model="checkbox1modelval' + scope.idvalue + value + key + '"> </td>'; 
       } 

       if (value == 5) { 
        trfirstelement = trfirstelement + '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="+" id="addbtn' + scope.idvalue + value + key + '" ng-click="addRow1(' + scope.c + ')">' + 
         ' </td>'; 
       } 

      }); 
      trfirstelement = trfirstelement + "</tr>"; 

      firstelement = angular.element(document.getElementById('tableid' + scope.idvalue)).append(trfirstelement); 
      $compile(firstelement)(scope); 

      trfirstelement = ''; 

      scope.addRow1 = function (a) { 
       scope.c++; 
       scope.count++; 

       trelements = '<tr id=mytr' + scope.idvalue + scope.c + '>'; 
       angular.forEach(scope.enableval, function (value, key) { 
        scope.keyvalue = key; 

        if (value == 1) { 
         trelements = trelements + '<td><input type="text" name="text1name' + scope.idvalue + scope.c + value + key + '" ng-model="text1value' + scope.idvalue + scope.c + value + key + '" id="text1valueeid' + scope.idvalue + scope.c + value + key + '" > </td>'; 
        } 
        if (value == 2) { 
         trelements = trelements + '<td><select id="selectid' + scope.idvalue + scope.c + value + key + '" name="selectname' + scope.idvalue + scope.c + value + key + '" ng-model="selectmodel' + scope.idvalue + scope.c + value + key + '" style="width:100%;"><option>..select..</option><option>Brother</option><option>Sister</option><option>Wife/Husband</option></select></td>'; 
        } 
        if (value == 3) { 
         trelements = trelements + '<td><input type="radio" id="radioid' + scope.idvalue + scope.c + value + key + '" ></input> </td>'; 
        } 
        if (value == 4) { 
         trelements = trelements + '<td><input type="checkbox" id="checkboxid' + scope.idvalue + scope.c + value + key + '" name="aaa" ></input> </td>'; 
        } 
        if (value == 5) { 
         trelements = trelements + '<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="+" id="addbtn' + scope.idvalue + scope.c + value + key + '" ng-click="addRow1(' + scope.c + ')">' + '&nbsp;&nbsp;&nbsp;<input type="button" value="-" ng-click="removerowbutton1(' + scope.c + ')" > </td>'; 
        } 
       }); 

       trelements = trelements + "</tr>"; 
       tablenm1 = angular.element(document.getElementById('tableid' + scope.idvalue)).append(trelements); 
       $compile(tablenm1)(scope); 
       trelements = ''; 
      } 

      scope.removerowbutton1 = function (index) { 
       //scope.c--; 
       alert("Removed") 
       var myEl = angular.element(document.querySelector('#mytr' + scope.idvalue + index)); 
       myEl.remove(); 
      } 
     } 
    }; 
}); 
+0

이 문제는 $ compile 때문에 발생한다고 생각합니다. 이것에 대한 대체품이 있습니까? –

답변

0

라인을 제거 할 필요가

$ 컴파일 (firstelement) (범위); $ compile (tablenm1) (scope);

으로 바꾸고 줄을 firstelement = angle.element (document.getElementById ('tableid'+ scope.idvalue))로 바꿉니다. append ($ compile (trfirstelement) (scope));

관련 문제