【问题标题】:Failed to instantiate module angularApp due to由于无法实例化模块 angularApp
【发布时间】:2017-12-11 22:37:24
【问题描述】:

当我在 angular js 和 symfony 之间进行身份验证时,我遇到了这个错误,我在前端(angular js)有错误:

未捕获的错误:[$injector:modulerr] http://errors.angularjs.org/1.6.5/$injector/modulerr?p0=angularApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org% 2F1.6.5%2F%24injector%2Fmodulerr%3Fp0%3Drestangular%26p1%3DTypeError%253A%2520Cannot%2520read%2520property%2520'isUndefined'%2520of%2520undefined%250A%2520%2520%2520%2520at%2520Object.Configurer. %2520(http%253A%252F%252Flocalhost%252Ftestauthenti%252Fnode_modules%252Frestangular%252Fdist%252Frestangular.js%253A42%253A29)%250A%2520%2520%2520%2520at%2520(new%2520%253E%52http%22 253A%252F%252Flocalhost%252Ftestauthenti%252Fnode_modules%252Frestangular%252Fdist%252Frestangular.js%253A812%253A16)%250A%2520%2520%2520%2520at%2520Object.instantiate%2520(https%25dnjscloud.2520(https%253A%252Frefla.com) %252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A44%253A458)%250A%2520%2520%2520%2520at%2520c%2520(https%253A%252F%252Fcdnjs.cloudflare.com% 252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A41%2 53A370)%250A%2520%2520%2520%2520at%2520Object.provider%2520(https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A41 %253A312)%250A%2520%2520%2520%2520at%2520d%2520(https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A42% 253A237)%250A%2520%2520%2520%2520at%2520https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A42%253A358%250A% %2520%2520%2520at%2520p%2520(https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A8%253A7)%250A%2520% 2520%2520%2520at%2520g%2520(https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A42%253A138)%250A%2520%2520 %2520%2520at%2520https%253A%252F%252Fcdnjs.cloudflare.com%252Fajax%252Flibs%252Fangular.js%252F1.6.5%252Fangular.min.js%253A42%253A322%0A%20%20%20%20at%20https %3A%2F%2Fcdnjs.cloudflare.com%2 Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min.js%3A7%3A76%0A%20%20%20%20at%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular。 js%2F1.6.5%2Fangular.min.js%3A43%3A70%0A%20%20%20%20at%20p%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js% 2F1.6.5%2Fangular.min.js%3A8%3A7)%0A%20%20%20%20at%20g%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1 .6.5%2Fangular.min.js%3A42%3A138)%0A%20%20%20%20at%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular。 min.js%3A42%3A322%0A%20%20%20%20at%20p%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min. js%3A8%3A7)%0A%20%20%20%20at%20g%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min.js %3A42%3A138)%0A%20%20%20%20at%20gb%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min.js% 3A46%3A251)%0A%20%20%20%20at%20c%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fa ngular.min.js%3A22%3A19)%0A%20%20%20%20at%20Uc%20(https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular .min.js%3A22%3A332)

这是链接错误:

https://docs.angularjs.org/error/$injector/unpr?p0=RestangularProvider%20%3C-%20Restangular%20%3C-%20MainCtrl

代码 index.html:

<!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">
        <!-- <link rel="stylesheet" href="styles/main.css"> -->
      </head>
      <body ng-app="angularApp">
        <div class="container">
        <div ng-include="'main.html" ng-controller="MainCtrl"></div>
        <div ui-view></div>
    <!--     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
     -->    <script src="../node_modules/angular/angular.js"></script>
    <!--     <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.js"></script>
     -->
        <script src="../node_modules/restangular/dist/restangular.js"></script>
        <script src="../node_modules/lodash/lodash.js"></script>

        <script src="app.js"></script>
        <script src="main.js"></script>
      </body>
    </html> 

代码 main.html:

<!—Displays error or success messages-->
            <span>{{message}}</span><br><br>

            <!—Login/logout form-->
            <form ng-show="!isAuthenticated" ng-submit="submit()">
                <label>Login Form:</label><br>
                <input ng-model="user.username" type="text" name="user" placeholder="Username" disabled="true" />
                <input ng-model="user.password" type="password" name="pass" placeholder="Password" disabled="true" />
                <input type="submit" value="Login" />
            </form>
            <div ng-show="isAuthenticated">
                <a ng-click="logout()" href="">Logout</a>
            </div>
            <div ui-view ng-show="isAuthenticated"></div>
            <br><br>

            <!—Displays contacts list-->
            <h1 ng-show="isAuthenticated">Liste des Contacts</h1>
            <article ng-repeat="contact in contacts" ng-show="isAuthenticated" id="{{ contact['@id'] }}" class="row marketing">
                <h2>{{ contact.nom }}</h2>
                <!—Displays contact phones list-->
                <h3 ng-repeat="moyenComm in contact.moyensComm">Tél : {{ moyenComm.numero }}</h3>
            </article><hr>

            <!—Create contact form-->
            <form name="createContactForm" ng-submit="createContact(createContactForm)" ng-show="isAuthenticated" class="row marketing">
                <h2>Création d'un nouveau contact</h2>
                <!—Displays error / success message on creating contact-->
                <div ng-show="contactSuccess" class="alert alert-success" role="alert">Contact publié.</div>
                <div ng-show="contactErrorTitle" class="alert alert-danger" role="alert">
                    <b>{{ contactErrorTitle }}</b><br>
                    {{ contactErrorDescription }}
                </div>
                <div class="form-group">
                    <input ng-model="newContact.nom" placeholder="Nom" class="form-control">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

            <!—Phone form-->
            <form name="createPhoneForm" ng-submit="createPhone(createPhoneForm)" ng-show="isAuthenticated" class="row marketing">
                <h2>Création d'un nouveau téléphone</h2>
                <div ng-show="phoneSuccess" class="alert alert-success" role="alert">Téléphone publié.</div>
                <div ng-show="phoneErrorTitle" class="alert alert-danger" role="alert">
                    <b>{{ phoneErrorTitle }}</b><br>
                    {{ phoneErrorDescription }}
                </div>
                <div class="form-group">
                    <input ng-model="newPhone.numero" placeholder="Numéro" class="form-control">
                </div>
                <div class="form-group">
                    <label for="contact">Contact</label>
                    <!—SelectBox de liste de contacts-->
                    <select ng-model="newPhone.contact" ng-options="contact['@id'] as contact.nom for contact in contacts" id="contact"></select>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

代码 app.js:

'use strict';

    var app = angular
        .module('angularApp', ['restangular'])
    app.config(['RestangularProvider', function (RestangularProvider) {
            // URL ENDPOINT TO SET HERE !!!
            RestangularProvider.setBaseUrl('http://your_vhost/api');

            RestangularProvider.setRestangularFields({
                id: '@id'
            });
            RestangularProvider.setSelfLinkAbsoluteUrl(false);

            RestangularProvider.addResponseInterceptor(function (data, operation) {
                function populateHref(data) {
                    if (data['@id']) {
                        data.href = data['@id'].substring(1);
                    }
                }

                populateHref(data);

                if ('getList' === operation) {
                    var collectionResponse = data['hydra:member'];
                    collectionResponse.metadata = {};

                    angular.forEach(data, function (value, key) {
                        if ('hydra:member' !== key) {
                            collectionResponse.metadata[key] = value;
                        }
                    });

                    angular.forEach(collectionResponse, function (value) {
                        populateHref(value);
                    });

                    return collectionResponse;
                }

                return data;
            });
        }])
    ;

代码 main.js:

'use strict';

    var app = angular
        .module('angularApp')
    app.controller('MainCtrl', function ($scope, $http, $window, Restangular) {
            // fosuser user
            $scope.user = {username: 'johndoe', password: 'test'};

            // var to display login success or related error
            $scope.message = '';

            // In my example, we got contacts and phones
            var contactApi = Restangular.all('contacts');
            var phoneApi = Restangular.all('telephones');

            // This function is launched when page is loaded or after login
            function loadContacts() {
                // get Contacts
                contactApi.getList().then(function (contacts) {
                    $scope.contacts = contacts;
                });

                // get Phones (throught abstrat CommunicationWays alias moyensComm)
                phoneApi.getList().then(function (phone) {
                    $scope.phone = phone;
                });

                // some vars set to default values
                $scope.newContact = {};
                $scope.newPhone = {};
                $scope.contactSuccess = false;
                $scope.phoneSuccess = false;
                $scope.contactErrorTitle = false;
                $scope.contactErrorDescription = false;
                $scope.phoneErrorTitle = false;
                $scope.phoneErrorDescription = false;

                // contactForm handling
                $scope.createContact = function (form) {
                    contactApi.post($scope.newContact).then(function () {
                        // load contacts & phones when a contact is added
                        loadContacts();

                        // show success message
                        $scope.contactSuccess = true;
                        $scope.contactErrorTitle = false;
                        $scope.contactErrorDescription = false;

                        // re-init contact form
                        $scope.newContact = {};
                        form.$setPristine();

                        // manage error handling
                    }, function (response) {
                        $scope.contactSuccess = false;
                        $scope.contactErrorTitle = response.data['hydra:title'];
                        $scope.contactErrorDescription = response.data['hydra:description'];
                    });
                };

                // Exactly same thing as above, but for phones
                $scope.createPhone = function (form) {
                    phoneApi.post($scope.newPhone).then(function () {
                        loadContacts();

                        $scope.phoneSuccess = true;
                        $scope.phoneErrorTitle = false;
                        $scope.phoneErrorDescription = false;

                        $scope.newPhone = {};
                        form.$setPristine();
                    }, function (response) {
                        $scope.phoneSuccess = false;
                        $scope.phoneErrorTitle = response.data['hydra:title'];
                        $scope.phoneErrorDescription = response.data['hydra:description'];
                    });
                };
            }

            // if a token exists in sessionStorage, we are authenticated !
            if ($window.sessionStorage.token) {
                $scope.isAuthenticated = true;
                loadContacts();
            }

            // login form management
            $scope.submit = function() {
                // login check url to get token
                $http({
                    method: 'POST',
                    url: 'http://your_vhost/login_check',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    data: $.param($scope.user)

                    // with success, we store token to sessionStorage
                }).success(function(data) {
                    $window.sessionStorage.token = data.token;
                    $scope.message = 'Successful Authentication!';
                    $scope.isAuthenticated = true;

                    // ... and we load data
                    loadContacts();

                    // with error(s), we update message
                }).error(function() {
                    $scope.message = 'Error: Invalid credentials';
                    delete $window.sessionStorage.token;
                    $scope.isAuthenticated = false;
                });
            };

            // logout management
            $scope.logout = function () {
                $scope.message = '';
                $scope.isAuthenticated = false;
                delete $window.sessionStorage.token;
            };

            // This factory intercepts every request and put token on headers
        })
        app.factory('authInterceptor', function($rootScope, $q, $window) {
        return {
            request: function (config) {
                config.headers = config.headers || {};

                if ($window.sessionStorage.token) {
                    config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                }
                return config;
            },
            response: function (response) {
                if (response.status === 401) {
                    // if 401 unauthenticated
                }
                return response || $q.when(response);
            }
        };
    // call the factory ...
    })
    app.config(function ($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    });

请帮助我解决此错误并感谢高级

【问题讨论】:

  • 我在你的代码中没有看到任何isUndefined?
  • 这似乎是来自 resctangular.js 的错误提示。 resctangular.js 是哪一个(app.js 或 main.js)?
  • 当我使用 cdn angular 1.6.5 时添加新错误时,我会更新我的问题

标签: javascript angularjs


【解决方案1】:

我认为isUndefined是underscore.js中的一个函数,是Restangular的一个依赖:

http://underscorejs.org/#isUndefined

您的资产中是否包含 underscore.js?

【讨论】:

猜你喜欢
  • 2018-09-29
  • 1970-01-01
  • 2018-09-10
  • 2014-05-20
  • 1970-01-01
  • 2017-06-17
  • 2017-02-18
  • 1970-01-01
  • 2023-03-27
相关资源
最近更新 更多