角度传递参数并接收json

时间:2016-06-16 17:49:47

标签: angularjs json

我正在使用此服务来获取json数据。我怎样才能传递两个参数。有了json,我需要填充一个下拉列表。

     myApp.service('allCurrentSettingsService', ['$http', '$q', function ($http, $q) {
        var allSettings = null;
        this.getList = function () {
            var def = $q.defer()
            if (allSettings) {
                def.resolve(allSettings);
            } else {
                $http.post('GetAllCurrentSettings', { companyName: compName, customerName: custName })
                  .then(function (response) {
                      var response = $.parseJSON(response.data)
                      allSettings = response;
                      def.resolve(allSettings);
                  });
            }
            return def.promise;
        }
    }]);

我在这里叫服务:

    myApp.controller('myController', ['$scope', 'getDocTypesService',
    function ($scope, getDocTypesService) {
    $scope.getDocTypes = '';
    getDocTypesService.getList(compName, custName).then(function (value ) {
    $scope.getDocTypes = value
    });
}

]);

1 个答案:

答案 0 :(得分:1)

你错过了功能参数。请在函数定义中添加相同的内容。

this.getList = function (compName, custName) {
            var def = $q.defer()
            if (allSettings) {
                def.resolve(allSettings);
            } else {
                $http.post('GetAllCurrentSettings', { companyName: compName, customerName: custName })
                  .then(function (response) {
                      var response = $.parseJSON(response.data)
                      allSettings = response;
                      def.resolve(allSettings);
                  });
            }
            return def.promise;
        }

您也可以通过其他方式调用$ http.post():

 var data = {param:'val1',.....}
    $http({
    url: "time.php",
    method: "POST",
    params: data
    })