考虑这个例子。我有一个画廊列表,每个画廊都选择了一个独特的客户。我也可以更改特定图库的客户端。为此,我在选择/选项列表中创建了一个客户列表。
<select ng-model="clientList" ng-options="client.id as client.clientName for client in clients" >
<option value="">Choose Client</option>
</select>
列表从DB填充。通常我使用client.id来选择项目。作为Angularjs的新手,似乎它将自己的值分配给value =“”。在我的示例中,gallery表有一个客户端列,列出了与客户端表和client.id相关的唯一clientID。如何选择正确的客户端?
控制器
function imageGalleryCtrl ($scope, images, clients, galleries)
{
$scope.panes = [
{ title:"Home", content:"/beta/application/views/images/uploader/create.html", active: true },
{ title:"Upload", content:"/beta/application/views/images/uploader/upload.html"},
{ title:"Edit", content:"/beta/application/views/images/uploader/edit.html"}
];
//close modal
$scope.close = function () {
$scope.imageUploader = false;
};
//get gallery info on click from table
$scope.getGallery = function(id, gallery)
{
//set gallery ID to scope
$scope.galleryID = id;
//open the modal
$scope.imageUploader = true;
//get gallery information
$scope.galleryCollection = galleries.getGallery(id);
$scope.galleryCollection.then(function(galleries){
$scope.gallery = galleries.thisGal;
});
//get clients
$scope.clientCollection = clients.getClients();
$scope.clientCollection.then(function(clients){
$scope.clients = clients.clients;
//Set client
});
//get all the images
$scope.imgCollection = images.getImages(id);
$scope.imgCollection.then(function(images){
$scope.images = images.thisGal_images;
});
};
}
服务
myApp.factory('galleries', function ($http, $q)
{
return {
getGallery: function (id)
{
var deferred = $q.defer(id);
$http.post('/beta/images/get/', {id: id}).success(function(data)
{
deferred.resolve(data);
});
return deferred.promise;
}
};
});
客户端服务几乎相同,仅引用正确的URL
感谢您的帮助
答案 0 :(得分:1)
它是模型驱动的,您需要将$ scope.clientList设置为客户端ID。在你的控制器中你可以做这样的事情
function Ctrl($scope) {
$scope.clients = [{
id: 1,
clientName: 'Joe'
}, {
id: 2,
clientName: 'Tom'
}, {
id: 3,
clientName: 'Bob'
}];
$scope.clientList = 2; //clientList is the model defined in the select directive
}
的 Demo on jsFiddle 强>
如果id
字段是字符串,则应将clientList
设置为字符串而不是整数:
$scope.clientList = "245";