gpt4 book ai didi

html - 自动填充 html 选择 AngularJS

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

我在使用 AngularJS 进行 HTML 选择时遇到了一点问题。当我向我的 API 请求时,我得到了一个整数值,但是当我尝试用它自动填充一个选择时,我无法正确设置 de“值”。

在这张图片中,您可以看到 HTML 正在接收什么以及我想要设置的值

Example of code

有什么方法可以转换这个值吗?

提前致谢:)

编辑:

获取客户数据并填写表格的 Controller

.controller('CustomerDetailCtrl', ['Customer', '$scope', '$sessionStorage', '$stateParams', '$ionicPopup', function (Customer, $scope, $sessionStorage, $stateParams, $ionicPopup) {
if ($sessionStorage.auth) {
Customer.get({data: $stateParams.customerId + '_' + $sessionStorage.user_id}).$promise.then(function (data) {
if (data.response && $sessionStorage.role === 1) {
$scope.customer = data.response[0];

if (data.history) {
$scope.histories = data.history;
}
} else {
console.log('Error de accesso...');
}
})
}

$scope.addTask = function (customer) {
alert('add task!');
}

$scope.deleteTask = function (customer, history) {
alert('delete task!');
}
}])

形式:

<label class="item item-input item-select">
<div class="input-label">
Cliente avisado?
</div>
<select name="informed" ng-model="customer.informed" required>
<option value="0">NO</option>
<option value="1">SI</option>
</select>
</label>

这是来自 de API 的数据图片: enter image description here

最佳答案

我知道您已经收到了这方面的答复,但我想向您展示另一种可能的选择,它不需要将您的数据从 int 更改为 string。如果您在 Controller 中定义您选择的选项(或者在服务中,如果这将在整个应用程序的多个不同位置使用),那么您可以利用 ng-options 及其使用能力字符串以外的值。

这是一个示例(显然我已经硬编码了一些东西并将它们全部放在一个模块中 - 而不是您在真实应用程序中所做的事情)。

JS:

angular.module('app', [])
.controller('ctrl', function($scope){
// select options (if these are common maybe store them in a service
// so you can share them in many controllers without duplicating the code)
$scope.selectOptions = [
{
text: 'NO',
value: 0
},
{
text: 'SI',
value: 1
}];
// sample data
$scope.customer = {
address: 'San Rosendo 11',
date: '2016-03-16T16:19:13+0100',
email: 'Montes',
equipment: 'PC',
id: 262,
informed: 1,
lastName: 'Montes',
location: 'Tienda',
name: 'Juanma',
notes: '',
pass: 'no tiene',
phone: '900112233',
price: '0',
status: 'Pendiente',
tasks: 'dfsdf'
};
});

HTML:

<div ng-app='app' ng-controller='ctrl'>
<select ng-model='customer.informed' ng-options='option.value as option.text for option in selectOptions'></select>
</div>

jsFiddle

关于html - 自动填充 html 选择 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36042164/

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