使用AngularJS将隐藏值传递给JSON

Pass hidden values to JSON using AngularJS

本文关键字:JSON 值传 隐藏 AngularJS 使用      更新时间:2023-09-26

我基本的开发人员,只是想通过隐藏值从我的html页面在json数组。下面是我的代码:-

index . html

  <!doctype html>
  <html ng-app="plunker" >
   <head>
     <meta charset="utf-8">
   <title>AngularJS Plunker</title>
   <script>document.write('<base href="' + document.location + '" />');
    </script>
   <link rel="stylesheet" href="style.css">
   <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
   <script src="app.js"></script>
    </head>
    <body>
    <div ng-controller="WorkingCtrl">
    <h1 ng-bind="vm.heading"></h1>   
    <form name="myForm" novalidate>
    <b> Text: </b>
    <input ng-model="form.text" required /><br>
    <button ng-click="submitForm()" bng-disabled="myForm.$invalid">Click Me!</button>
    </form>
    <p ng-bind="showValues"></p>
     </div>
     </body>
     </html>

app.js

    var app = angular.module('myApp');
    app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {
    $scope.form = {};
    $scope.submitForm = function () {
     $scope.showValues = JSON.stringify($scope.form);
    }
  }]);

现在,来自ng-model="form.text"的值成功返回了未隐藏输入类型

的结果
       { "text":"What_ever_text" }  

,但如何发送数组中定义的值,包括隐藏的输入类型,如:-

  <input value="99999" type="hidden" ng-model="form.userid" required />

所以,想要的输出像:-

  { "text":"What_ever_text" , "userid":"99999" } 

有答案吗?注:-输入类型应隐藏或任何其他更容易的方法赞赏。

隐藏输入字段的工作原理与普通字段相同。

见下面的代码

  var app = angular.module('myApp', []);
  app.controller('WorkingCtrl', ['$scope', '$http', function ($scope, $http) {
      $scope.form = {
          text: "some test",
          userid: '12312312312'
      };
      $scope.submitForm = function () {
          $scope.showValues = JSON.stringify($scope.form);
      }
  }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="WorkingCtrl">
         <h1 ng-bind="vm.heading"></h1> 
        <form name="myForm" ng-submit="submitForm()" novalidate>Text:
            <input ng-model="form.text" required />
            <br/>
            <input type="hidden" ng-model="form.userid" required />
            <button bng-disabled="myForm.$invalid">Click Me!</button>
        </form>
        <p ng-bind="showValues"></p>
    </div>
</div>