繁体   English   中英

单击Angularjs中的保存按钮时如何从动态创建的输入文本框中获取数据

[英]How to fetch data from dynamically created input textboxes on click of save button in Angularjs

我正在为每个项目动态创建文本框和保存按钮,单击保存按钮后,我需要从该特定项目的文本框中获取值。

 for (let d = 0; d <= ItemsInfo.length - 1; d++)
    {                 
           content += '<tr>  <td> <label for="lblPriority">Item Priority </label>  </td>  ';
           content += ' <td>   <input type="text" id="inpItemPRIORITY" ng-model="prty" value=" ' +  ItemsInfo[d].PRIORITY  + ' " /> </td> </tr>';
           content += '<tr>  <td>  <label for="lblItemComment">Item Comment</label></td> ';
           content += ' <td>   <input type="text" id="inpItemCOMMENT"  ng-model="cmnt"  value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>';
            // Save Item
          content += '<tr>  <td>  <button class="get-data" ng-click="buttonClick(prty,cmnt)">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
  }

在控制器中:

 $scope.buttonClick = function (prty,cmnt) {
  console.log(prty + " " + cmnt); } // console.log displays as undefined undefined

还是有更好的方法来做到这一点?

首先,正确的AngularJs应该是直接在HTML模板中使用ngRepeat ,但是我仍然会为您提供当前代码的解决方案:

首先,HTML ID应该是唯一的,您正在控制器中的数组上手动循环,因此您可以在ID中添加索引号,并将其赋予函数以查找元素(还修复了for循环条件以进行优化) ):

在这种情况下,绑定到模型是没有用的,否则所有输入将使用相同的作用域变量。 此代码还显示了在AngularJS中以jQlite格式检索DOM元素的正确方法:

for (let d = 0; d < ItemsInfo.length; d++)
{                 
    content += '<tr>  <td> <label for="lblPriority">Item Priority </label>  </td>  ';
    content += ' <td>   <input type="text" id="inpItemPRIORITY_' + d + '" value="' +  ItemsInfo[d].PRIORITY  + '" /> </td> </tr>';
    content += '<tr>  <td>  <label for="lblItemComment">Item Comment</label></td> ';
    content += ' <td>   <input type="text" id="inpItemCOMMENT_' + d + '"  value="' + ItemsInfo[d].COMMENT + '" /> </td> </tr>';
    // Save Item
    content += '<tr>  <td>  <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> ';
}
$scope.buttonClick = function (index) {
    let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ),
        cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) );
    console.log(prty.val() + " " + cmnt.val());
};

这是一个演示片段。 只是一个显示其工作原理的示例(编辑:添加了$compile指令, buttonClick才能正常工作)。

 angular.module('selectExample', []) .controller('ExampleController', ['$scope', function($scope) { const ItemsInfo = [ { ITEM_ID: 'a1', COMMENT: 'comment a1', PRIORITY: 0 }, { ITEM_ID: 'b1', COMMENT: 'comment b1', PRIORITY: 1 }, { ITEM_ID: 'c1', COMMENT: 'comment c1', PRIORITY: 2 }, ]; let content = ''; for (let d = 0; d < ItemsInfo.length; d++) { content += '<tr> <td> <label for="lblPriority">Item Priority </label> </td> '; content += ' <td> <input type="text" id="inpItemPRIORITY_' + d + '" value=" ' + ItemsInfo[d].PRIORITY + ' " /> </td> </tr>'; content += '<tr> <td> <label for="lblItemComment">Item Comment</label></td> '; content += ' <td> <input type="text" id="inpItemCOMMENT_' + d + '" value=" ' + ItemsInfo[d].COMMENT + ' " /> </td> </tr>'; // Save Item content += '<tr> <td> <button class="get-data" ng-click="buttonClick(' + d + ')">Save Item(' + ItemsInfo[d].ITEM_ID + ')</button> </td> </tr> '; } $scope.htmlContent = content; $scope.buttonClick = function (index) { let prty = angular.element( document.querySelector( '#inpItemPRIORITY_' + index ) ), cmnt = angular.element( document.querySelector( '#inpItemCOMMENT_' + index ) ); console.log(prty.val() + " " + cmnt.val()); }; }]) .directive('bindHtmlCompile', ['$compile', function ($compile) { return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { element.html(value); $compile(element.contents())(scope); }); } }; }]); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular-sanitize.min.js"></script> <div ng-app="selectExample" ng-controller="ExampleController"> <table bind-html-compile="htmlContent"></table> </div> 

暂无
暂无

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

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