使用ng-options在select元素中绑定数据后,从编辑功能设置所选值,显示错误"无法设置属性' UID'未定义" 。但是,如果我从select元素中选择除默认值以外的其他值,则编辑函数的值会发生变化。
这是我的选择元素代码:
<select name="userRole" id="userRole" ng-model="userRoleModel" ng-options="r.Role for r in roles track by r.UID" class="form-control select-1">
<option value="" ng-selected="true">ROLES</option>
</select>
Angular JS功能:
**//--Binding data to select element--//**
$scope.GetRoles = function()
{
$http({
methhod:"get",
url:"/Admin/GetRoles",
datatype:"json"
}).then(function(response){
var jData = JSON.parse(response.data);
$scope.roles = jData.ResultData;
},function(response){
console.log(response.data)
})
};
**//--Edit function--//**
$scope.GetUserUpdateData = function(userData){
$scope.userUID = userData.UID;
$scope.userName = userData.Name;
$scope.userUsername = userData.Username;
$scope.userPassword = userData.Password;
$scope.userRoleModel.UID = userData.RoleID;
$scope.userRoleModel.Role = userData.Role;
$scope.example2model = userData.ChannelUIDs;
}
JSON DATA
{"ResultData":[{"UID":"1","Role":"Super Admin"},{"UID":"2","Role":"Admin"},{"UID":"2b660a63-c038-4bcd-8fcc-616af7cce4a9","Role":"Operator"},{"UID":"6","Role":"Customer"}]}
请注释,而不是此代码。请注释。
答案 0 :(得分:1)
从角色中选择对象作为默认值,而不是对象内的属性。由于默认值映射到ng-options中的对象值。
<select
ng-value="r"
ng-model="userRoleModel"
ng-options="r.Role for r in roles track by r.UID" >
<option value="?" selected="selected">
</option>
</select>
然后在控制器中选择对象所需的默认值。
$scope.userRoleModel = $scope.roles[1];
答案 1 :(得分:1)
您在编辑功能中遇到此错误,因为在分配它的嵌套属性值之前未定义Username = (AutoCompleteTextView) findViewById(R.id.username);
Username.setAdapter(adapter);
。
此行userRoleModel
出错,因为$scope.userRoleModel.UID = userData.RoleID;
不是$scope.userRoleModel
。首先在编辑函数之外声明为defined
之类的空对象,然后才能工作。
喜欢:
$scope.userRoleModel = {}
但最好使用像
这样的参考$scope.userRoleModel = {}
$scope.GetUserUpdateData = function (userData) {
$scope.userUID = userData.UID;
$scope.userName = userData.Name;
$scope.userUsername = userData.Username;
$scope.userPassword = userData.Password;
$scope.userRoleModel.UID = userData.RoleID;
$scope.userRoleModel.Role = userData.Role;
$scope.example2model = userData.ChannelUIDs;
}