javascript - Angular 服务参数错误

标签 javascript angularjs

我正在尝试将两个变量从 Angular 服务发送到 mvc Controller 。我总是遇到错误。现在我收到 compName 不存在的错误。我已经堆积了几个小时,我尝试使用 Angular 调试器对其进行调试,但到目前为止还没有成功。 我认为我的错误与 Angular Controller 试图在加载时调用服务而不是在单击时调用服务有关,但我不知道如何解决这个问题。

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

这是我的 Angular Controller 和服务:

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

          })

          };
      }
      ]);

这是 HTML: <select ng-model = "selectedDocument" ng-click="getDocTypes(selectedCompany, enteredCustomer)"> <option>Select document</option> <option ng-repeat="docSetting in docSettings" value=" {{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option> </select>

最佳答案

试试这个:

Controller :

$scope.getTypes = function(comp, cust) {
    getDocTypesService.getDocTypes(comp, cust).then(function (value) {
          $scope.docSettings = value
    });
};

模板:

<select ng-model = "selectedDocument" ng-click="getTypes(selectedCompany, enteredCustomer)">

观察:selectedCompany 和 EnteredCustomer 必须是在点击发生之前定义的 $scope 变量。

关于javascript - Angular 服务参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879035/

相关文章:

javascript - 如何在 controller.js 中使用过滤器比较日期

angularjs - 在最小输入字符集后显示 ui-select 下拉列表

javascript - 表格行文本突出显示弹出窗口(仅)我

JavaScript TypeError,无法读取未定义的属性,但函数已定义。我究竟做错了什么?

javascript - 如何通过 React 通过 key 返回对象内容(JSON 文件)?

javascript - 如何获取两个属性的值并将其放入另一个属性?

javascript - 如何使用jquery从 Angular 服务获取json数据

javascript - Controller 中的 Angular 函数构造函数

javascript - Angularjs - 类型错误 : Object #<Scope> has no method 'path'

javascript - 如何在进入下一页之前更改按钮的文本?