繁体   English   中英

为什么我没有在 html 中显示 ajax 表?

[英]i am not displaying ajax table in html why?

我正在尝试使用 AngularJS 和 Ajax 运行一个应用程序。 我还使用了一个返回 JSON 的 Java 控制器。 不幸的是,html 不显示表格,我不明白这是怎么回事,请帮忙。 我刚刚开始使用 Ajax 和 angularJS。 我正在尝试在 Html 中输出一个表,这是我的文件:

框架.js

var app = angular.module('frames', []);

app.controller("FramesController", function ($scope, $http) {

    $scope.successGetFramesCallback = function (response) {
        $scope.frameList = response.data;
        $scope.errormessage="";
    };

    $scope.errorGetFramesCallback = function (error) {
        console.log(error);
        $scope.errormessage="Unable to get list of frames";
    };

    $scope.getFrames = function () {
        $http.get('/mcga/frames').then($scope.successGetFramesCallback, $scope.errorGetFramesCallback);
    };

});

框架.html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>

    <meta charset="utf-8"></meta>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
    <title>Schools in our university</title>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
    <script src="js/frames.js"></script>

</head>
<body ng-app="frames" ng-controller="FramesController">

<h1>Компьютерные корпуса</h1>

<div ng-controller="getFrames" ng-show="frameList.length > 0">
    <table id="frameTable">
        <thead>
        <tr>
            <th><h2>Id</h2></th>
            <th><h2>Company</h2></th>
            <th><h2>Model</h2></th>
            <th><h2>Price</h2></th>
            <th><h2>Amount</h2></th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="frame in frameList" id="{{frame.id}}">
            <td>{{frame.id}}</td>
            <td>{{frame.company}}</td>
            <td>{{frame.model}}</td>
            <td>{{frame.price}}</td>
            <td>{{frame.amount}}</td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

我的控制器是用 java 编写的,它从数据库中获取列表并以 JSON 返回它,它工作正常。 JSON 返回给我。 这是代码:

@RestController
public class MyController {

    @Autowired
    private Service service;

    @GetMapping("mcga/frames")
    public ResponseEntity<Object> showAllFrames()
    {
        List<frame> frameList=service.getAllFrames();
        return ResponseEntity.ok(frameList);

    }
}

还有我的 ConfigController:

@Configuration
public class ControllerConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/frames").setViewName("frames");
    }
}

你放错了 ng-controller。 它必须是 FramesController:像这样的东西

<div ng-controller="FramesController" ng-show="frameList.length > 0">
    <table id="frameTable">
        <thead>
        <tr>
            <th><h2>Id</h2></th>
            <th><h2>Company</h2></th>
            <th><h2>Model</h2></th>
            <th><h2>Price</h2></th>
            <th><h2>Amount</h2></th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="frame in frameList" id="{{frame.id}}">
            <td>{{frame.id}}</td>
            <td>{{frame.company}}</td>
            <td>{{frame.model}}</td>
            <td>{{frame.price}}</td>
            <td>{{frame.amount}}</td>
        </tr>
        </tbody>
    </table>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM