AngularJS仅存储嵌入对象ID

时间:2015-03-27 12:27:23

标签: angularjs nested jsonobject

我一直坚持某些事情,也许我一直在搜索错误的关键字,所以没有找到其他类似的线程。

我正在构建一个以angularjs作为前端的应用程序,REST后端使用简单的模型。

Subtrack.java
  Private String subtrackname

Track.java
  Private String trackname
  Private Subtrack subtrack

Presentation.java
  Private String title
  Private String presenter
  Private Track track

定义了所有REST接口(使用JBoss Forge从上面的POJO构建它们。我还使用Forge搭建了Angular)但我似乎无法找到Angular表单来创建一个新的Presentation,我将它关联起来跟踪演示文稿。

我有一个已定义的轨道列表(可能有一个子轨道,我放入新的Presentation控制器的列表中......

$scope.sessionTracksSelectionList = TrackResource.queryAll(function() {});
$scope.sessionSubtracksSelectionList = {};

if ($scope.presentation.sessiontrack != null) {
    $scope.sessionSubtracksSelectionList = $scope.presentation.sessiontrack.subtracks;
}

然后我的新演示文稿表格如下所示。

<div class="form-group" ng-class="{'has-error': PresentationForm.sessiontrack.$invalid}">
    <label for="sessiontrack" class="col-sm-2 control-label">Sessiontrack</label>
    <div id="sessiontrackControls" class="col-sm-10">
        <select id="sessiontrack" name="sessiontrack" class="form-control" ng-model="presentation.sessiontrack" ng-options="s.trackname for s in sessionTracksSelectionList track by s.id">
            <option value="">Choose a session track</option>
        </select>
    </div>
</div>

<div class="form-group" ng-class="{'has-error': PresentationForm.sessionsubtrack.$invalid}">
    <label for="sessionsubtrack" class="col-sm-2 control-label">Sessionsubtrack</label>
    <div id="sessionsubtrackControls" class="col-sm-10">
        <select id="sessionsubtrack" name="sessionsubtrack" class="form-control" ng-model="presentation.sessionsubtrack" ng-options="s.subtrackname for s in sessionSubtracksSelectionList track by s.id">
            <option value="">Choose a session subtrack</option>
        </select>
    </div>
</div>

这似乎适用于在表示对象中传递正确的信息:

{
  room: {id: 9, version: 0, roomname: "santa maria 1"}
  sessionday: 3
  sessionsubtrack: {id: 3, version: 0, subtrackname: "cloud"}
  sessiontrack: {id: 4, version: 0, trackname: "ps", subtracks: [{id: 3, version: 0, subtrackname: "cloud"}, {id: 1, version: 0, subtrackname: "mw"}, {id: 2, version: 0, subtrackname: "inf"}]}
  title: "p3"
}

然而,它传递的是整个Track对象,而不仅仅是ID,所以我得到一个“列Track的值太长的错误”,因为它只允许最多255个字符而且我不想增加大小,因为我不想在数据库中复制所有这些内容。

那么,有没有办法让我只存储我可以在请求演示时检索的Track的ID?

非常感谢。

1 个答案:

答案 0 :(得分:0)

更改ngOptions格式,以便在模型中写入曲目ID:

<select id="sessiontrack" name="sessiontrack" class="form-control" 
        ng-model="presentation.sessiontrack" 
        ng-options="s.id as s.trackname for s in sessionTracksSelectionList track by s.id">
    <option value="">Choose a session track</option>
</select>