0
我一直在研究一個項目,並試圖使用AngularJs創建一個下拉菜單。但我無法獲得菜單上的employeId信息來選擇。如果需要更多的代碼細節,我可以添加。AngularJS - 使用ngRepeat的下拉菜單
下面是它的定義之前,image of dropdownmenu
Delete.html
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="drop">
{{selectedItem}}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li ng-repeat="a in emps">
<a ng-click="dropboxitemselected(a)">{{a.Id}}</a>
</li>
</ul>
</div>
Demo.js
MyApp.controller("DeleteController", function ($scope, EmpApi) {
$scope.selectedItem = "Select Employee";
$scope.isDeleteItemVisible = false;
getEmployees();
function getEmployees() {
EmpApi.getEmployees().then(function (response) {
$scope.emps = emps;
})
.catch(function (error) {
$scope.status = 'Unable to load emp data:' + error.message;
})
};
$scope.dropboxitemselected = function (item) {
$scope.selectedItem = item.Id;
$scope.name = item.Name;
$scope.age = item.Age;
$scope.sal = item.Salary;
$scope.empid = item.Id;
$scope.isDeleteItemVisible = true;
};
$scope.DeleteEmp = function() {
var empToDelete = {
'Id': $scope.empid
};
EmpApi.DeleteEmployee(empToDelete)
.then(function (response) {
alert("user deleted");
$scope.name = undefined;
$scope.age = undefined;
$scope.sal = undefined;
$scope.empid = undefined;
$scope.selectedItem = "Select Employee";
$scope.isDeleteItemVisible = false;
getEmployees();
})
.catch(function (response) {
alert("error in deleting");
});
}
});
不知道這是問題的一部分,但在你的'getEmployees'功能,你要指定'$ scope.emps = emps',但是你沒有在'.then'回調函數中傳遞'emps'作爲參數。你正在傳遞'response'。 – Andrew