javascript - 无法访问其中一个 ng 模型的 Angular

标签 javascript angularjs

我正在使用 angular,我已经遇到这个问题几天了。当我尝试提交表单时,我的第二个下拉列表的值为空(selectedDocument 下拉列表)。几天前我发布了我的代码,但没有人能帮助我。这就是为什么我再次重新放置代码。

<div  ng-controller="myController">
<form name="myForm" >
    <div>
        <select ng-model="selectedCompany">
            <option value="">-- Select Company --</option>
            <option data-ng-repeat="currentSetting in currentSettings" value={{currentSetting.SCACCode}}>{{currentSetting.SCACCode}}</option>
        </select>
    </div>
    <div><input id="Text1" type="text"  ng-model="enteredCustomer"/></div>
    <div>
        <select ng-model="selectedDocument" ng-click="getTypes()">
            <option value="">-- Select Doc type --</option>
            <option data-ng-repeat="docSetting in docSettings" value="{{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option>
        </select>
    </div>
    <input id ="btnAdd" type="button" value="Add new record" ng-click="setNewRecord()"/>
</form>

这是我的javascript

<script>
    var myApp = angular.module("myApp", []);

    myApp.service('companiesService', ['$http', '$q', function ($http, $q) {
        var currentSettings = null;

        this.getList = function () {
            var def = $q.defer()
            if (currentSettings) {
                def.resolve(currentSettings);
            } else {
                $http.post('GetCompanies')
                  .then(function (response) {
                      var response = $.parseJSON(response.data)
                      currentSettings = response;
                      def.resolve(currentSettings);
                  });
            }
            return def.promise;
        }
    }]);

    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')
                  .then(function (response) {
                      var response = $.parseJSON(response.data)
                      allSettings = response;
                      def.resolve(allSettings);
                  });
            }
            return def.promise;
        }
    }]);

    myApp.service("deleteService", function ($http) {
        this.removeRow = function (recId, compName, custName, docName) {

            $http.post('DeleteRecord', { settingID: recId,companyName: compName,customerName: custName, documentName: docName } )
            .success(function (data, status, headers, config) {
               window.location.reload();
            })
            .error(function (data, status, header, config) {
            });
        }
    });

    myApp.service("setNewRecordService", function ($http) {
        this.setNewRecord = function (compName, custName, docName) {


            $http.post('SetCurrentRecord', { companyName: compName, customerName: custName, documentType: docName })
            .success(function (data, status, headers, config) {


                window.location.reload();
            })
            .error(function (data, status, header, config) {
            });
        }
    });

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





    myApp.controller('myController', ['$scope', 'companiesService', 'allCurrentSettingsService','deleteService', 'setNewRecordService', 'getDocTypesService',
      function ($scope, companiesService, allCurrentSettingsService, deleteService, setNewRecordService, getDocTypesService) {

          $scope.currentSettings = '';
          companiesService.getList().then(function (value) {
              $scope.currentSettings = value;

          }),
          $scope.allSettings = '';
          allCurrentSettingsService.getList().then(function (value) {
              $scope.allSettings = value;

          }),
          $scope.deleteRecordFromDB = function (recId, compName, custName, docName) {
              deleteService.removeRow(recId, compName, custName, docName)
          },

          $scope.companyName = '';
          $scope.setNewRecord = function () {
              setNewRecordService.setNewRecord($scope.selectedCompany, $scope.enteredCustomer, $scope.selectedDocument)

          },

          $scope.getTypes = function () {
              getDocTypesService.getDocTypes($scope.selectedCompany, $scope.enteredCustomer).then(function (value) {
                  $scope.docSettings = value
              });
          };
            }
    ]);

最佳答案

您可能与 angular(以及与此相关的 Javascript)处理范围的方式有关。

简而言之,问题在于子作用域正在断开与父作用域(您的 Controller 变量)的连接。一个简单的解决方法是将您的表单变量绑定(bind)到一个对象,并在 View 中使用点符号来引用它们。

您可以在许多 SO 答案中阅读此内容,例如: Why don't the AngularJS docs use a dot in the model directive?

编辑

我做了一个minimal working plunkr这应该会为您指明正确的方向,这是经过编辑的代码。

myApp.controller('myController', ['$scope', 'companiesService', 'allCurrentSettingsService','deleteService', 'setNewRecordService', 'getDocTypesService',
  function ($scope, companiesService, allCurrentSettingsService, deleteService, setNewRecordService, getDocTypesService) {

      $scope.selections = {company: null, document: null};

      $scope.currentSettings = '';
      companiesService.getList().then(function (value) {
          $scope.currentSettings = value;

      }),
      $scope.allSettings = '';
      allCurrentSettingsService.getList().then(function (value) {
          $scope.allSettings = value;

      }),
      $scope.deleteRecordFromDB = function (recId, compName, custName, docName) {
          deleteService.removeRow(recId, compName, custName, docName)
      },

      $scope.companyName = '';
      $scope.setNewRecord = function () {
          setNewRecordService.setNewRecord($scope.selected.company, $scope.enteredCustomer, $scope.selected.document)

      },

      $scope.getTypes = function () {
          getDocTypesService.getDocTypes($scope.selected.company, $scope.enteredCustomer).then(function (value) {
              $scope.docSettings = value
          });
      };
        }
]);

和 html:

<div  ng-controller="myController">
    <form name="myForm" >
    <div>
        <select ng-model="selected.company">
            <option value="">-- Select Company --</option>
            <option data-ng-repeat="currentSetting in currentSettings" value={{currentSetting.SCACCode}}>{{currentSetting.SCACCode}}</option>
        </select>
</div>
<div><input id="Text1" type="text"  ng-model="enteredCustomer"/></div>
<div>
    <select ng-model="selected.document" ng-click="getTypes()">
        <option value="">-- Select Doc type --</option>
        <option data-ng-repeat="docSetting in docSettings" value="{{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option>
    </select>
</div>
<input id ="btnAdd" type="button" value="Add new record" ng-click="setNewRecord()"/>

关于javascript - 无法访问其中一个 ng 模型的 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37920202/

相关文章:

javascript - 为什么Webpack要注入(inject)React

javascript - 为什么 Protractor 在自动初始化的 Angular 站点上找不到 Angular?

html - 错误 :Failed to execute 'atob' on 'Window' : The string to be decoded is not correctly encoded

javascript - ng-options 为每个选项添加后缀

javascript - 图形中的蒙特卡洛方法

javascript - Highcharts 渲染器 x 和 y 在 Firefox 与 Chrome 上不同

javascript - 如何使用 jquery 获取下一页/上一页

javascript - 将 Google "My Maps"图层添加到 Google map Javascript API

javascript - 在 AngularJS 中,如何对 `$http` 的所有响应应用检查函数?

angularjs - Angular 形式对未选择的选择有效