我正在尝试完成项目的CRUD,但是我遇到了问题。
当我尝试绑定到模式EmployeeDetail.DeptID
以选择Department
时,它失败了。这是我的代码
HTML
<div ng-model="EmployeeDetail">
<input type="text" ng-model="EmployeeDetail.EmpName" placeholder="Name" />
<select id="Departments" ng-selected="//Some thing in here!">
<option ng-repeat="dept in Departments" value="{{dept.DeptId}}">{{dept.DeptName}}</option>
</select>
<input type="text" ng-model="EmployeeDetail.Address" placeholder="Address" />
<input type="text" ng-model="EmployeeDetail.Email" placeholder="Mail" />
<select>
<option ng-selected="EmployeeDetail.Gender==true" value="true">Male</option>
<option ng-selected="EmployeeDetail.Gender==false" value="false">Female</option>
</select>
<input type="text" ng-model="EmployeeDetail.YearOfBirth" />
<button value="create" ng-click="createEmployee()" ng-show="create">Submit</button>
<button value="update" ng-click="updateEmployee()" ng-show="update">Update</button>
<button value="cancel" ng-click="cancelEmployee()" ng-show="cancel">Cancel</button>
</div>
JS
var loadData = function () {
$http.get(urlE).then(function (response) {
var obj = JSON.parse(response.data);
$scope.Employees = obj;
$('#empTable').DataTable();
});
$http.get(urlD).then(function(response){
var obj = JSON.parse(response.data);
$scope.Departments = obj;
})
}
loadData();
$scope.edit = function (emp) {
$http({
method: 'GET',
url: urlE + "/" + emp.EmpID
}).then(function (response) {
var obj = response.data;
$scope.EmployeeDetail = obj;
console.log(obj);
$scope.updateEmployee = function () {
$http({ method: 'POST', url: "api/Employees/Update", data: obj }).then(function (response) {
alert("Edit success!");
loadData();
});
}
});
}
我绑定了Employee
才能正确输入,但是下拉列表Departments
无法绑定。我想将Employee.DeptId
绑定到select id="Departments"
并显示以查看具有ID的DepartmentsName
。我开始使用AngularJS和我不理解的东西。您能为我解释一下,并告诉我正确的方法吗?非常感谢!
答案 0 :(得分:0)
使用ng-model和选项
<select id="Departments" ng-model="Employee.DeptId" ng-options="dept.DeptId as dept.DeptName for dept in Departments"></select>