0

I have the following code:

<select class="form-control" data-ng-model="form.ProtocolId" data-ng-options="protocol.ProtocolId as protocol.Name for protocol in protocols" required />

Where a protocol has this structure:

[{ "ProtocolId": 1, "Name": "Protocol 1", "Description": "A description for protocol 1" }]

In $scope.form I am setting the ProtocolId, which is what I need to submit the form. But I would like to show the description of the Protocol to the user when they select it. Sort of like {{selectedProtocol.Description}}. Except I don't have a selectedProtocol as I'm only selecting the id.

Is there a way to get the actual selected Protocol here? I could write a method to track the change of a selected Protocol and then in my controller set the form.ProtocolId, but I wonder if there is a simpler way...

4

2 回答 2

0

我倾向于不使用该ng-options指令,select因为通常我还想做其他事情......

<select class="form-control" ng-model="form.protocol" required>
    <option ng-repeat="protocol in protocols" value="protocol">{{protocol.Name}}</option>
</select>
<div ng-show="form.protocol.Description">{{form.protocol.Description}}</div>

现在,当使用form.protocol所选协议的 JS 对象进行选择时。您可以稍后对其进行操作,以便form.protocol.ProtocolID在提交时发送。

于 2013-10-11T19:00:57.790 回答
0

因此,我将 ngRepeat 选项标记为答案,但由于以下几个原因我最终没有使用它:主要是由于我的模型结构,我的列表值没有在我的表单中正确填充 angularjs。然而,DID 起作用的是:

<select class="form-control" data-ng-model="form.PrimaryProtocolId" data-ng-options="protocol.ProtocolId as primaryDisplayName(protocol) for protocol in primaryprotocols" required />

我的 $scope 上的方法在哪里primaryDisplayName()提供了我需要的格式:

$scope.primaryProtocolName = function(protocol) {
    return protocol.Name + " (" + protocol.ProtocolType.Name + ")";
};
于 2013-10-14T18:00:18.070 回答