如何通过弹簧休息中的anuglar获取显示值?

时间:2017-05-13 06:59:55

标签: angularjs spring spring-restcontroller

我有两个实体DiscDiscLog

光盘实体:

@Entity
@Table(name = "disc")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Disc implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne
    private Connection connection;

    @OneToMany(mappedBy = "disc", fetch = FetchType.EAGER)
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<DiscLog> discLogs = new HashSet<>();

 ...
}

DiscLog实体:

@Entity
@Table(name = "disc_log")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class DiscLog implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @NotNull
    @Column(name = "util", nullable = false)
    private Double util;

    @NotNull
    @Column(name = "svctim", nullable = false)
    private Double svctim;

    @NotNull
    @Column(name = "await", nullable = false)
    private Double await;

    @NotNull
    @Column(name = "avgqusz", nullable = false)
    private Double avgqusz;

    @NotNull
    @Column(name = "avgrqsz", nullable = false)
    private Double avgrqsz;

    @Column(name = "date", insertable = false, columnDefinition = "now()")
    private ZonedDateTime date;

    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    private Disc disc;

    ...

我正在使用angularJS,因此我的部分HTML看起来像:

         <dd>
            <span>{{vm.disc.name}}</span>
        </dd>

如何显示例如光盘 - &gt; discLog - &gt; util值? 我试过了

     <dd>
        <span>{{vm.disc.disclog.util}}</span>
    </dd>

并且没有显示任何内容。

我的休息看起来像:

   @GetMapping("/discs/{id}")
    @Timed
    public ResponseEntity<Disc> getDisc(@PathVariable Long id) {
        log.debug("REST request to get Disc : {}", id);
        Disc disc = discRepository.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(disc));
    }

Anuglar服务:

(function() {
    'use strict';
    angular
        .module('deviceManagerApp')
        .factory('Disc', Disc);

    Disc.$inject = ['$resource'];

    function Disc ($resource) {
        var resourceUrl =  'api/discs/:id';

        return $resource(resourceUrl, {}, {
            'query': { method: 'GET', isArray: true},
            'get': {
                method: 'GET',
                transformResponse: function (data) {
                    if (data) {
                        data = angular.fromJson(data);
                    }
                    return data;
                }
            },
            'update': { method:'PUT' }
        });
    }
})();

2 个答案:

答案 0 :(得分:0)

光盘文件有一组Set对象。因此,您必须遍历前端中每个Disc对象中的每个DiscLog对象。

因此您无法使用{{vm.disc.disclog.util}}直接访问discLog。而是使用:

{{vm.disc.disclog [i] .util}}其中i = 0,1,2,3 ...

示例:

<sometag ng-repeat="i in objectsLength"> {{vm.disc.disclog[i].util}}</sometag>

答案 1 :(得分:0)

好吧,您的disc包含一组discLogs。 所以你必须打电话:

<dd>
    <span>{{vm.disc.discLogs[0].util}}</span>
</dd>