无法从使用 AngularJS 动态创建的文本框的值后面的代码中看到

Cannot see from code behind the values of Dynamically created textboxes using AngularJS

我是 AngularJS 编程新手。任何帮助将不胜感激。

  1. 每次单击 HTML 按钮时都会创建一个 HTML 文本框。
  2. webform1.aspx提交按钮中点击甚至无法捕获在那些文本框中输入的值。

我使用了 request.form,在 form1 中循环遍历控件,但找不到动态创建的控件。

如何post并从代码隐藏中查看在这些文本框中输入的数据?

请查找以下代码:

webform1.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
      <script src="https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js"></script>
   </head>
   <body>
      <form id="form1" runat="`server">
         <div>
         <div ng-app="myApp" ng-controller="myCtrl">
            <li ng-repeat="element in elements ">
               <input type="text" ng-model="element.id" runat=server/>
            </li>
            <input type="button" value="+" ng-click="newItem()" />
         </div>
      </form>
   </body>
   <script language="JavaScript">
      var app = angular. Module('myApp', []);
      app.controller('myCtrl', function ($scope) {
          var counter = 0;     
          $scope.elements = [{ id: counter, value: ''}];

          $scope.newItem = function () {
              if ($scope.elements[counter].value != '') {
                   counter++;
                   var str1 = 'txtdynamic';
                   str1 += counter;
                   $scope.elements.push({ id: str1, value: '' });
              }
          }
      });
   </script>
</html>

webform1.aspx.cs

protected void Button2_Click(object sender, EventArgs e)
{
    //get the html ng repeat textbox control values
}

尝试正确的方法,我的朋友。将以下代码与您的方法进行比较。

> demo fiddle.

查看

<div ng-controller="myCtrl">
  <form id="form1" runat="`server">
    <div>
      <div ng-app="myApp" ng-controller="myCtrl">
        <li ng-repeat="element in elements">
          <input type="text" ng-model="elements[element.id].value" runat=server/>
        </li>
        <input type="button" value="+" ng-click="newItem()" />
        <input type="button" value="send" ng-click="ButtonClick()" />
      </div>
    </div>
  </form>
</div>

AngularJS申请

  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) {

    var counter = 0;

    $scope.elements = [{
      id: counter,
      value: ''
    }];

    $scope.newItem = function() {
      if ($scope.elements[counter].value != '') {
        counter++;
        var str1 = 'txtdynamic';
        str1 += counter;
        $scope.elements.push({
          id: str1,
          value: ''
        });
      }
    }

    $scope.ButtonClick = function() {
      var post = $http({
        method: "POST",
        url: "/Home/AjaxMethod",
        dataType: 'json',
        data: {
          id: angular.isDefined($scope.elements[0]) ? $scope.elements[0].value : null
        },
        headers: {
          "Content-Type": "application/json"
        }
      });
    }
  });