使用 $http 配置 ngRoute

Configure ngRoute with $http

我正在尝试使用 $http 配置 ngRoute,但当我访问网站时,控制台出现错误:localhost:8080

Uncaught ReferenceError: angularModule is not defined(anonymous function) @ item_controller.js:2 angular.js:38Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.5/$injector/modulerr?p0=myApp&p1=Error%3A%2…2Flocalhost%3A8080%2Fbower_components%2Fangular%2Fangular.min.js%3A21%3A19)

当我打开 http://localhost:8080/home 时,我会看到 "Whitable error page"。我正在 Spring 框架中编写我的 API (Spring-boot)

我应该如何配置这两个东西才能正常工作?我找不到任何好的例子我应该如何写得好。

我的配置如下所示:

app.js

'use strict';

var App = angular.module('myApp', ['ngRoute', 'moduleItemController']);

App.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider
            .when( '/home', {
                templateUrl: '../item.html',
                controller: 'ItemController'
            }).
            when('/item/:itemCode',{
                templateUrl: '../../templates/item-detail.html',
                controller: 'ItemController'
            }).otherwise({
                redirectTo: '/home'
        });
    }]);

item_controller.js

'use strict';
var moduleItemController = angularModule.module('moduleItemController',[])

moduleItemController.controller('ItemController', ['$scope', 'ItemService', function ($scope, ItemService) {
    var self = this;
    self.item = {id: null, itemCode: '', itemName: '', price: ''};
    self.items = [];

    self.fetchAllItems = function () {
        ItemService.fetchAllItems()
            .then(
                function (d) {
                    self.items = d;
                },
                function (errResponse) {
                    console.error('Error while fetching Currencies');
                }
            );
    };

    self.createItem = function (item) {
        ItemService.createItem(item)
            .then(
                self.fetchAllItems,
                function (errResponse) {
                    console.error('Error while creating item.');
                }
            );
    };

    self.updateItem = function (item, id) {
        ItemService.updateItem(item, id)
            .then(
                self.fetchAllItems,
                function (errResponse) {
                    console.error('Error while updating item.');
                }
            );
    };

    self.deleteItem = function (id) {
        ItemService.deleteItem(id)
            .then(
                self.fetchAllItems,
                function (errResponse) {
                    console.error('Error while deleting item.');
                }
            );
    };


    self.updatePrice = function (newPrice, item, id) {
        ItemService.updatePrice(newPrice, item, id)
            .then(
                self.fetchAllItems,
                function (errResponse) {
                    console.error('Error while updating item.');
                }
            );
    };


    self.fetchAllItems();

    self.submit = function () {
        if (self.item.id === null) {
            console.log('Saving New item', self.item);
            self.createItem(self.item);
        }
        else {
            console.log('Sth went wrong!');
        }
        self.reset();
    };


    self.remove = function (id) {
        console.log('id to be deleted', id);
        if (self.item.id === id) {
            self.reset();
        }
        self.deleteItem(id);
    };


    self.reset = function () {
        self.item = {id: null, itemCode: '', itemName: '', price: ''};
        $scope.myForm.$setPristine();
    };

}]);

moduleItemController.controller('ItemController', ['$scope','$routeParams', function ($scope,$routeParams) {
    $scope.itemCode = $routeParams.itemCode;
}]);

item_service.js

App.factory('ItemService', ['$http', '$q', function ($http, $q) {

    return {

        fetchAllItems: function () {
            return $http.get('http://localhost:8080/api/item/all')
                .then(
                    function (response) {
                        return response.data;
                        console.success('Success');
                    },
                    function (errResponse) {
                        console.error('Error while fetching users');
                        return $q.reject(errResponse);
                    }
                );
        },
.
.
.
// other crud operations

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HOME</title>

    <link rel="stylesheet" type="text/css" href="bower_components/semantic/dist/semantic.min.css">
    <script src="bower_components/semantic/dist/semantic.js"></script>

    <!--Angular dependencies-->
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controller/item_controller.js"></script>
    <script src="js/service/item_service.js"></script>

</head>
<body ng-app="myApp">
 <div ng-view></div>
</body>
</html>

item.html——这是我想使用 ngRoute

的文件的一部分
<table class="ui fixed table">
        <thead>
        <tr>
            <th>Id</th>
            <th>Code</th>
            <th>Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th width="20%"></th>
            <th width="20%"></th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="i in ictrl.items | filter:query">
            <td><span ng-bind="i.id"></span></td>
            <td class="selectable">
                <a href="#/item/{{i.itemCode}}"><span ng-bind="i.itemCode"></span></a>
            </td>
            <td><span ng-bind="i.itemName"></span></td>
            <td><span ng-bind="i.price"></span></td>
            <td><span ng-bind="i.quantity"></span></td>
            <td>
                <button class="negative ui button" ng-click="ictrl.remove(i.id)">delete</button>
            </td>
            <td>
                <button class="ui secondary button">zp</button>
            </td>
        </tr>
        </tbody>
    </table>

项目-detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Details</title>

    <!--SEMANTIC-->
    <link rel="stylesheet" type="text/css" href="bower_components/semantic/dist/semantic.min.css">
    <script src="bower_components/semantic/dist/semantic.js"></script>

    <!--ANGULAR-->
    <script src="bower_components/angular/angular.min.js"></script>
    <script src="bower_components/angular-route/angular-route.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controller/item_controller.js"></script>
    <script src="js/service/item_service.js"></script>

</head>
<body ng-app="myApp">

<div class="item table container">
    <h1>TBD DETAILED VIEW FOR {{itemCode}}</h1>
</div>

</body>
</html>

item_controller.js 的第 2 行引用了未定义的 angularModule.module。我想你想要 angular.module.

问题是您试图在 item_controller.js 中使用 angularModule 而不是 angular。来自 angular.js 的全局对象是 angular 而不是 angularModule

将下面的行更改为:

var moduleItemController = angularModule.module('moduleItemController',[]);

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