AngularJS:分配和读取两个不同提交按钮的值

我有一个带有两种提交输入类型的表单。根据单击的提交,我想将表单保存为草稿或正确的表单。


我正在考虑向提交输入添加一个值/属性,也许是 ng-model 属性。


<form ng-submit="submitNew()">

  <div>

    Text:

    <input type="text" ng-model="myForm.text" />

  </div>

  <div>

    <input type="submit" name="correct" value="Add new" />

  </div>

  <div>

    <input type="submit" name="draft" value="Save as draft" />

  </div>

  </div>

</form>

在控制器中,我想将其作为布尔值(“myForm.isDraft”)添加到使用 POST 方法发送的数据中:


myApp.controller('myAppController', ['$scope', '$http', '$log',

  function($scope, $http, $log) {

    $scope.submitNew = function() {

      $http({

        method: 'POST',

        url: '/app/submit',

        data: {

          text: $scope.myForm.text,

          isDraft: $scope.myForm.isDraft,

        }

      })

    };

  }

]);

它如何处理提交类型的输入?


叮当猫咪
浏览 120回答 2
2回答

GCT1015

尝试将其放在类似的函数上$scope.setDraft = function(isDraft) {&nbsp; $scope.isDraft = isDraft;&nbsp; //try console.log() to see if the values are correct&nbsp; //but if its keeps return false try adding this $scope.$apply();}在你的html中会是这样的<div>&nbsp; <input type="submit" ng-click="setDraft(false)" value="Add new" /></div><div>&nbsp; <input type="submit" ng-click="setDraft(true)" value="Save as draft" /></div>或者您可以使用 @Eric 建议而不是使用 ng-submit,只需创建新函数并在 ng-click 上使用,如下所示$scope.submitNew = function(isDraft) {&nbsp; $http({&nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; url: '/app/submit',&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; text: $scope.myForm.text,&nbsp; &nbsp; &nbsp; isDraft: isDraft,&nbsp; &nbsp; }&nbsp; })};在html中会是这样的<form>&nbsp; <div>&nbsp; &nbsp; Text:&nbsp; &nbsp; <input type="text" ng-model="myForm.text" />&nbsp; </div>&nbsp; <div>&nbsp; &nbsp; <input type="button" ng-click="submitNew(false)" name="correct" value="Add new" />&nbsp; </div>&nbsp; <div>&nbsp; &nbsp; <input type="button" ng-click="submitNew(true)" name="draft" value="Save as draft" />&nbsp; </div>&nbsp; </div></form>

汪汪一只猫

创建一个通过在每个提交按钮上单击 ng 来设置的变量,然后您可以跟踪单击了哪个按钮。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5