1

考虑这个例子。我有一个画廊列表,每个画廊都选择了一个独特的客户。我还可以更改特定画廊的客户。为此,我在选择/选项列表中创建了一个客户列表。

<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=""。在我的示例中,图库表有一个客户列,列出了与客户表和 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

谢谢您的帮助

控制台日志

4

1 回答 1

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";
于 2013-07-30T15:53:00.517 回答