javascript - Angular $http POST 无法绑定(bind)数组

标签 javascript angularjs rest

所以我尝试使用 Angular 设置 post 函数。我有一个 HTML 表单,其中有两个文本框(用于用户输入)和一个用于选择多个选项的下拉菜单(以便用户填写表单并向服务器提交数据)。

绑定(bind)两个文本框很好,但我不知道如何将数组中的两个选项绑定(bind)为下拉菜单中的选项?

(这是一个 fiddle :http://jsfiddle.net/gtv7s8h3/2/)

表格:

<form>
<input type="text" id="name" ng-model="myForm.Title" ng-minlength="5" ng-maxlength="20"> title <br/>
<input type="text" id="name" ng-model="myForm.Content" ng-minlength="5" ng-maxlength="20"> content <br />

<select ng-model="CategoryId" ng-options="item.name for item in CategoryId"></select>

<button ng-click="myForm.submitTheForm()">Submit Form</button>
</form>

Angular 发布:

 angular.module("myapp", [])
 .controller("MyController", function($scope, $http) {
  $scope.myForm = {};
  $scope.myForm.Title = "";
  $scope.myForm.Content = "";
  $scope.CategoryId = {
    data: [{
        id: '316556404cac4a6bb47dd4c7ca2dac4a',
        name: 'name1'
    }, {
        id: '306e3d9a6265480d94d0d50e144435f9',
        name: 'name2'
    }]
  };       

 $scope.myForm.submitTheForm = function(item, event) {
 var dataObject = {
 Title : $scope.myForm.Title,
 Content : $scope.myForm.Content,
 CategoryId : $scope.CategoryId 

   };

   var responsePromise = $http.post("/url", dataObject, {});
   responsePromise.success(function(dataFromServer, status, headers, config) {
      console.log(dataFromServer.title);
   });
    responsePromise.error(function(data, status, headers, config) {
      alert("Submitting form failed!");
   });
 }
  });

最佳答案

您正在尝试将 categoryID 绑定(bind)到您的数组,而您的 ngOptions 表达式不会循环遍历您的数组。您需要将 categoryId 值绑定(bind)到不同的模型。

为您的categoryID添加模型:

$scope.myForm.categoryId = null;

并更改您的选择标记:

<select ng-model="myForm.categoryId" ng-options="item.id as item.name for item in CategoryId.data"></select>

关于javascript - Angular $http POST 无法绑定(bind)数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31777902/

相关文章:

javascript - 当 $scope 被销毁时删除事件监听器

javascript - Angular : ng-repeat. 我如何根据值定位特定项目

python - 按多个属性过滤的 Mixpanel 数据导出

javascript - 不传递任何参数时不需要括号

javascript - CSS3 和 Javascript : fade text out and then in using the same function

javascript - document.body 在 IE 中不起作用

javascript - JSON 到 jQuery/JavaScript 的日期格式问题

javascript - 如果范围是通过 ajax 填充的,则 AngularJS 指令模板不会更新

php - 如何在php中撤销用户对微软APP的访问权限

javascript - 防止用户操纵 REST URL