简体   繁体   English

ng模型之一的角度无法访问

[英]Angular on one of the ng models not accessible

I am working with angular and I am having this issue for a few days. 我正在与angular一起工作,几天来一直遇到这个问题。 When I tried to submit the form the value of my second drop down is null(selectedDocument dropdown). 当我尝试提交表单时,第二个下拉列表的值为null(selectedDocument下拉列表)。 I posted my code a few days back but nobody could help me. 我几天前发布了代码,但没有人可以帮助我。 That is why I am reposing the code again. 这就是为什么我要重新放置代码。

<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>

And this is my javascript 这是我的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
              });
          };
            }
    ]);

Is the dropdown get data or not 下拉列表是否获取数据

if not 如果不

i think in getTypes function ,can you try this 我认为在getTypes函数中,你可以试试这个吗

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

In your controller you have, for example, this: 例如,在您的控制器中,您可以:

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

What's the comma for? 什么是逗号?

End your call with a ; 以结束通话; and with all the others too. 以及所有其他人。

Your first select has the data from that first service call. 您的第一个选择具有来自该第一个服务呼叫的数据。 Then it errors out because of all the comma'd functionality after it. 然后由于它后面所有的逗号功能而出错。 Terminate them correctly, and then when it gets down to settings $scope.docSettings it should be correct. 正确终止它们,然后当它变为设置$ scope.docSettings时应该正确。

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)
    };

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

Something like that, my ES5 is a little rusty, but see if that works. 像这样,我的ES5有点生锈,但是看看是否可行。 Edited: removed the unnecessry docTypes. 编辑:删除不必要的docType。

Your might have something something to do with the way angular(and Javascript for that matter) handles scopes. 您可能与angular(以及有关此问题的Javascript)处理范围的方式有关。

The short of it is that the problem is that a child scope is breaking the connection to the parent scope(your controller variable). 简而言之,问题在于子作用域断开了与父作用域(您的控制器变量)的连接。 A simple fix is to bind your form variables to an object and refer to them with the dot notation in the view. 一个简单的解决方法是将表单变量绑定到对象,并在视图中使用点符号引用它们。

You can read up on this in numerous SO answers, for example here: Why don't the AngularJS docs use a dot in the model directive? 您可以在许多SO答案中对此进行阅读,例如,在这里: 为什么AngularJS文档在model指令中不使用点?

Edit 编辑

I made a minimal working plunkr that should point you in the right direction, and here is the edited code. 我做了一个最小的工作插件 ,应该指出正确的方向,这是编辑后的代码。

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
          });
      };
        }
]);

and html: 和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()"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM