Javascript 刷新后缺少选择框项
我正在尝试设置下拉框显示默认项。首先,下拉框中的项目能够正确显示,我可以选择该项目以保存到数据库中。一切正常,问题是在我刷新页面后,默认项总是空的,它应该显示我保存的项Javascript 刷新后缺少选择框项,javascript,angularjs,html,Javascript,Angularjs,Html,我正在尝试设置下拉框显示默认项。首先,下拉框中的项目能够正确显示,我可以选择该项目以保存到数据库中。一切正常,问题是在我刷新页面后,默认项总是空的,它应该显示我保存的项 HTML <select class="form-control" ng-model="sst_type" ng-options="type for type in sst_types" ng-change="changeSstType(sst_type)"></select> 您需要从服务器返回
HTML
<select class="form-control" ng-model="sst_type" ng-options="type for type in
sst_types" ng-change="changeSstType(sst_type)"></select>
您需要从服务器返回已保存项目的id。可以在Module.getSst()调用中执行此操作。 在foreach中,将$scope.sst_type设置为匹配的类型
祝你好运为了打开它,你需要获取保存在数据库中的数据,并将其设置为select上的模式。ng model=“sst_type”这样它将设置所选选项(如果该值与sst_type中的值类似)
.controller('sstCtrl', function ($scope, $rootScope, Module, settings,
toastr, actionBar, $uibModal) {
// scope properties
$scope.loading = true;
$scope.sst_types = ['Sotong', 'Bento'];
$scope.sst_all = [];
// scope functions
$scope.getSst = function () {
Module.getSst().then(function (res) {
$scope.loading = false;
if (res.sst_all) {
angular.forEach(res.sst_all, function (v) {
$scope.sst_all.push({
id: v.id,
name: v.name,
value: v.value,
percentage: (v.value * 100).toString(),
type: v.value == 0 ? 'Sotong' : 'Bento'
});
});
}
});
};