angularjs $ resource.save上的未定义响应。$ promise回调

时间:2017-11-06 23:09:32

标签: javascript angularjs json

我正在试图在API上发帖。其中一个值我必须从一个从json填充的下拉列表中获取它。这是我从json获取值的代码的一部分:

function comboNation (){
  $http.get('app/componentes/comboProcedence.json')
    .then(function(data){
      vm.nation = data.data;
    //console.log(vm.nation);
  });
}

编辑而且响应只是两个值。这是Json数据:

    [
  {
    "Id": 1,
    "value": "National"
  },
  {
    "Id": 2,
    "value": "International"
  }
]

html视图:

<div class="col-md-4">Procedence
  <br>
  <button type="button" id="options2" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">
    {{$ctrl.btnProcedence}} 
    <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" id="list2">
    <li  ng-repeat="alo in $ctrl.nation">
      <a href="#" ng-click="$ctrl.getWhereIs(alo)">{{alo.value}}</a>
    </li>
  </ul>
</div>

现在,使用下一个代码,我将获得所选的值:

vm.btnProcedence = "Select one";

vm.getWhereIs = function(f){
  vm.isThere = f.Id;
  vm.btnProcedence = f.value;
  //console.log(vm.isThere);
}

vm.idDoc = vm.isThere;

在控制台日志中,我使用vm.isThere打印所选值。如您所见,嵌套在ng-click上。此时,它工作正常,但是当我试图发布帖子时,所选的值是 undefined

这是我正在发布帖子的代码的一部分:

vm.travelData = function() {
  var data = {
    Name: vm.name,
    IdDocumentType: vm.idDoc,
  };
  loadInfo(data);
  console.log(data);
};

function loadInfo(data) {
  var arr = {
    Name: vm.name,
    IdDocumentType: vm.idDoc,
  };
  postInfo.save(arr).$promise
    .then(function(response) {
      console.log(response);
    });
}

Html视图:

<button type="button" class="btn btn-primary" ng-click="$ctrl.travelData()">Send Info</button>

有人知道为什么未定义?

api的响应是真的,因此,没有错误信息。

希望你能帮助我。

提前完成。

1 个答案:

答案 0 :(得分:1)

vm.idDoc = vm.isThere;必须位于vm.getWhereIs函数内。另外:vm.nameundefined

见这个例子:

(function() {
  var app = angular.module("app", []);
  app.controller("Controller", ["$scope", function($scope) {
    $scope.nation = [{
        "Id": 1,
        "value": "National"
      },
      {
        "Id": 2,
        "value": "International"
      }
    ];

    $scope.btnProcedence = "Select one";

    $scope.getWhereIs = function(f) {
      $scope.isThere = f.Id;
      $scope.btnProcedence = f.value;
      $scope.idDoc = $scope.isThere;
      $scope.name = "A name...";
    };

    $scope.travelData = function() {
      var data = {
        Name: $scope.name,
        IdDocumentType: $scope.idDoc,
      };
      loadInfo(data);
    };

    function loadInfo(data) {
      var arr = {
        Name: $scope.name,
        IdDocumentType: $scope.idDoc,
      };
      console.log(arr);

      /* postInfo.save(arr).$promise
         .then(function(response) {
           console.log(response);
         });
         */
    }
  }]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="app">
  <div data-ng-controller="Controller">
    <div class="col-md-4">Procedence
      <br>
      <button type="button" id="options2" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="btn btn-default dropdown-toggle">
    {{btnProcedence}} 
    <span class="caret"></span>
  </button>
      <ul class="dropdown-menu" id="list2">
        <li ng-repeat="alo in nation">
          <a href="#" data-ng-click="getWhereIs(alo)">{{alo.value}}</a>
        </li>
      </ul>
    </div>
    <button type="button" class="btn btn-primary" data-ng-click="travelData()">Send Info</button>

  </div>
</div>