[英]Angular, Select, Set Default
I know this has probably been asked multiple times, but I feel like I am doing everything right, but I can't seem to set the default option for a select using AngularJS. 我知道这个问题可能已经被问过多次了,但是我觉得我所做的一切都很好,但是我似乎无法使用AngularJS为select设置默认选项。
I'm using : AngularJS, Angular-UI-router, Bootstrap 我正在使用 :AngularJS,Angular-UI-router,Bootstrap
Relevant HTML : 相关HTML :
<select ng-model="selectedCompany" ng-options="c.name for c in companies" class="form-control"></select>
Juice Service : 果汁服务 :
vapelog.factory('Juices', function($http, $q){
var Juices = {
get : function() {
var promise = $http.get('/juices').then(function(data){
return data.data;
});
return promise;
},
show : function(id) {
var deferred = $q.defer();
$http.get('/juices/'+id).then( function ( juice ) {
$http.get('/companies').then( function ( companies ) {
juice.companies = companies;
deferred.resolve( juice );
}, function getAcctError() { deferred.reject(); } );
}, function getUserError() { deferred.reject(); } );
return deferred.promise;
},
}
return Juices;
});
In my controller : 在我的控制器中 :
vapelog.controller('JuiceDetailCtrl', ['$scope','$stateParams','Juices',function($scope, $stateParams, Juices) {
var id = $stateParams.juice;
$scope.juice = {};
$scope.selectedCompany = {};
Juices.show(id).then(function(juice){
$scope.juice = juice.data;
$scope.companies = juice.companies.data;
$scope.reviews = $scope.juice.reviews;
$scope.selectedCompany = $scope.juice.company;
}, function(){
console.log('error');
});
$scope.tfIcon = function(item){
if(item == "1"){
return 'glyphicon-ok';
} else {
return 'glyphicon-remove';
}
}
}]);
Everything prepopulates, the select box contains all the items, but it doesn't pre-select the item. 一切都将预先填充,选择框将包含所有项目,但不会预选择该项目。 It starts with a blank select option. 它以空白选择选项开头。
The fact that the $scope.companies variable is set and populating the select options makes me think that the $http.get() has already returned successfully, and therefore the $scope.selectedCompany should already be populated too. 设置$ scope.companies变量并填充选择选项的事实使我认为$ http.get()已经成功返回,因此$ scope.selectedCompany也应该已经填充。 I may be wrong, though. 我可能是错的。
If someone can see where I am going wrong, and can enlighten me, that would be awesome. 如果有人可以看到我要去哪里出了问题并可以启发我,那真是太棒了。
$scope.juice.company is an object that is from the javascript point of view not part of the array that contains the companies. 从javascript的角度来看,$ scope.juice.company是一个对象,它不是包含公司的数组的一部分。 You have to find the correct companie by yourself in the companies. 您必须自己在公司中找到正确的伙伴。 For exapmle, assumed that the company has a property id: 例如,假设公司具有财产编号:
$scope.selectedCompany = findCompany($scope.juice.company, $scope.companies);
function findCompany(theCompany, companies){
var result;
angular.forEach(companies, function(companieInArray){
if(companieInArray.id === theCompany.id){
result = companieInArray;
}
});
return result;
}
You can use the id as the model in the select and then set this in the controller: 您可以在选择中将id用作模型,然后在控制器中进行设置:
app.controller('Ctrl', function ($scope) {
var opentr;
$scope.juices = [];
$scope.juice = 2;
$scope.juices.push({"id": 1 , "fruit": "orange"});
$scope.juices.push({"id": 2 , "fruit": "apple"});
$scope.juices.push({"id": 3 , "fruit": "pear"});
$scope.juices.push({"id": 4 , "fruit": "pinapple"});
});
html: HTML:
{{ juice }}
<select ng-model="juice" ng-options="juice.id as juice.fruit for juice in juices"></select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.