ng-options和更新模型

时间:2013-08-01 04:51:52

标签: angularjs ng-options

在以下情况下,当选择客户端时,我想在库模型中更新。如何使用所选客户端更新图库模型?

<form novalidate>
            <div class="control-group">
                <label for="galleryName">Gallery Name:</label>
                <input value="{{gallery.galleryName}}" id="galleryName" type="text" ng-model="gallery.galleryName">
                <p>{{gallery}}</p>
            </div><!-- /control-group -->

            <div class="control-group">
                <label for="clientName">Client Name:</label>
                <select name="client" ng-model="clientList" ng-options="client.id as client.clientName for client in clients" >
                    <option value="">Choose Client</option>
                </select>
                {{clientList}}
            </div>
        </form>

图库模型示例:

{"id":"57","galleryName":"Sam","clientRef":"205","client":"245","favorited":"1","timestamp":"1374524146"}

我的目标是在更改clientList时更改“客户端”。

1 个答案:

答案 0 :(得分:1)

你可以使用$scope.$watch,你可以做这样的事情

$scope.$watch('clientList', function (oldValue, newValue) {
    $scope.gallery.client = newValue; //newValue is the client.id
});

顺便说一句,您应该将clientList重命名为其他内容,因为您选择的只是一个项目。

相关问题