我有两个下拉菜单和一张桌子。 我不知道如何将所选值从第一个下拉列表传递到第二个下拉列表。除非未将选定值传递给第二个下拉列表,否则第一个服务正常工作。
我的JavaScript:
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('docTypeService', ['$http', '$q', function ($http, $q) {
var docSettings = null;
this.getList = function () {
var def = $q.defer()
if (docSettings) {
def.resolve(docSettings);
} else {
$http.post('GetDocTypes')
.then(function (response) {
var response = $.parseJSON(response.data)
docSettings = response;
def.resolve(docSettings);
});
}
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.controller('myController', ['$scope', 'companiesService', 'docTypeService', 'allCurrentSettingsService',
function ($scope, companiesService, docTypeService, allCurrentSettingsService) {
$scope.currentSettings = '';
companiesService.getList().then(function (value) {
$scope.currentSettings = value;
});
$scope.docSettings = '';
docTypeService.getList().then(function (value) {
$scope.docSettings = value;
});
$scope.allSettings = '';
allCurrentSettingsService.getList().then(function (value) {
$scope.allSettings = value;
});
}
]);
我的HTML`
<table>
<tr>
<td>Select a company:</td>
<td>
<div data-ng-controller="myController">
<select ng-model="dropdownvalue" ng-change="GetDocTypes(dropdownvalue)">
<option data-ng-repeat="currentSetting in currentSettings" value={{currentSetting.SCACCode}}>{{currentSetting.SCACCode}}</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Enter customer: </td>
<td><div><input id="Text1" type="text" /></div></td>
</tr>
<tr>
<td>Select document:</td>
<td>
<div ng-controller="myController">
<select>
<option>Select document</option>
<option ng-repeat="docSetting in docSettings" value="{{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option>
</select>
</div>
</td>
</tr>
</table>
`
我的MVC控制器
`
public JsonResult GetDocTypes(string company)
{
var jsonString = string.Empty;
string query = "SELECT * FROM Companies WHERE CompName = @Company";
try
{
using (SqlConnection conn = new SqlConnection(connStringEbe))
{
conn.Open();
using (SqlCommand command = new SqlCommand(query, conn))
{
DataTable dt = new DataTable();
command.Parameters.Add(new SqlParameter("@Company", company));
using (SqlDataReader rdr = command.ExecuteReader())
{
dt.Load(rdr);
}
command.Parameters.Clear();
jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(dt, Formatting.None);
return Json(jsonString);
}
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
return Json(jsonString);
}
}
`