gpt4 book ai didi

javascript - 如何使用参数 AngularJs onChange 下拉值

转载 作者:行者123 更新时间:2023-11-28 18:13:29 26 4
gpt4 key购买 nike

我有两个从 Rest Service 获取的下拉列表。第一个下拉列表不使用参数。第二个下拉列表使用第一个下拉列表中的参数。

此代码用于获取休息服务

service.js

app.service("GetCategoryService", function ($http) {
this.GetCategory = function () {
return $http.get("http://localhost:51458/ServiceRequest.svc/GetCategory/");
};

this.GetSubCategory = function (category) {
return $http.get("http://localhost:51458/ServiceRequest.svc/GetSubCategory/" + category);
};
});

此代码用于 Controller 到 html

entryCtrl.js

(function(app) {
'use strict';

app.controller('entryCtrl', entryCtrl);

entryCtrl.$inject = ['$scope', 'GetCategoryService'];

function entryCtrl($scope, GetAllEntryService, GetCategoryService) {
$scope.pageClass = 'page-entry';
//To Get Category
$scope.Category = function() {
var promiseGet = GetCategoryService.GetCategory();
promiseGet.then(function(pl) {
$scope.GetCategory = pl.data
},
function(errorPl) {
console.log('Some Error in Getting Records.', errorPl);
});
}
$scope.Category();

//To Get Sub Category
$scope.SubCategory = function() {
var promiseGet = GetCategoryService.GetSubCategory();
promiseGet.then(function(pl) {
$scope.GetSubCategory = pl.data
},
function(errorPl) {
console.log('Some Error in Getting Records.', errorPl);
});
}
$scope.SubCategory();
}
})(angular.module('entry'));

entry.html

<div class="form-group">
<label class="control-label col-sm-2" for="category">Category:</label>
<div class="col-sm-4">
<div class="dropdown">
<select class="form-control" ng-model="Category" ng-init="Category = Category[0]" ng-options="item.ITEM_TEXT for item in GetCategory">
<span class="caret"></span>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="subCategory">Sub Category:</label>
<div class="col-sm-10">
<div class="dropdown">
<!--I Don't Know what must I fill this-->
</div>
</div>
</div>

第一个下拉列表的输出服务,例如[{"ITEM_TEXT":"APP"},{"ITEM_TEXT":"IT"}]

第二个下拉列表/子类别的输出服务,例如[{"ITEM_TEXT":"TROUBLESHOOTING"},{"ITEM_TEXT":"REQUEST NEW USER"}]

帮助第二个下拉菜单的onChange

最佳答案

我想我明白你现在想做的事情。

entryCtrl.js

(function(app) {
'use strict';

app.controller('entryCtrl', entryCtrl);

entryCtrl.$inject = ['$scope', 'GetCategoryService'];

function entryCtrl($scope, GetAllEntryService, GetCategoryService) {
$scope.pageClass = 'page-entry';

//Set initial Category and Subcategory to be empty
$scope.Category = '';
$scope.SubCategory = '';

//To Get Category
$scope.GetCategory = function() {
var promiseGet = GetCategoryService.GetCategory();
promiseGet.then(function(pl) {
//Set categories data to the response
//This is an array of objects
$scope.Categories = pl.data;
},
function(errorPl) {
console.log('Some Error in Getting Records.', errorPl);
});
}
//Fetch the categories, sets $scope.Categories
$scope.GetCategory();

//To Get Sub Category
$scope.GetSubCategory = function(category) {
//If user picks "Select Subcategory", do nothing
if (category === '') return false;

//Unset SubCategories so the select box is not shown until loading is done
$scope.SubCategories = null;

var promiseGet = GetCategoryService.GetSubCategory(category);
promiseGet.then(function(pl) {
//Set subcategories
$scope.SubCategories = pl.data;
},
function(errorPl) {
console.log('Some Error in Getting Records.', errorPl);
});
}
}
})(angular.module('entry'));

entry.html

<div class="form-group" ng-show="!!Categories">
<label class="control-label col-sm-2" for="category">Category:</label>
<div class="col-sm-4">
<div class="dropdown">
<select class="form-control" id="category" ng-model="Category" ng-change="GetSubCategories(Category)">
<option value="">Select Category</option>
<option ng-repeat="CategoryObject in Categories" value="{{CategoryObject.ITEM_TEXT}}">{{CategoryObject.ITEM_TEXT}}</option>
</select>
</div>
</div>
</div>
<div class="form-group" ng-show="!!SubCategories">
<label class="control-label col-sm-2" for="subCategory">Sub Category:</label>
<div class="col-sm-10">
<div class="dropdown">
<select class="form-control" id="subCategory" ng-model="SubCategory">
<option value="">Select Subcategory</option>
<option ng-repeat="SubCategoryObject in SubCategories" value="{{SubCategoryObject.ITEM_TEXT}}">{{SubCategoryObject.ITEM_TEXT}}</option>
</div>
</div>
</div>

关于javascript - 如何使用参数 AngularJs onChange 下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41155018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com