【问题标题】:i am not displaying ajax table in html why?我没有在 html 中显示 ajax 表,为什么?
【发布时间】:2021-12-08 15:34:55
【问题描述】:

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

Frames.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);
    };

});

frames.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");
    }
}

【问题讨论】:

    标签: javascript java html angularjs


    【解决方案1】:

    你放错了 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>
    

    【讨论】:

    • 谢谢,我的错误更透明,我错误地指定了js文件的路径
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2021-07-20
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多