由于

时间:2017-07-07 15:01:47

标签: javascript angularjs

当我在角度js和symfony之间进行身份验证时,我有这个bug,我的前端有bug(角度js):

  

未捕捉错误:[$ injector:modulerr] http://errors.angularjs.org/1.6.5/ $ injector / modulerr?p0 = angularApp& p1 =错误%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.init%2520(HTTP%253A%252F%252Flocalhost%252Ftestauthenti%252Fnode_modules%252Frestangular%252Fdist%252Frestangular.js%253A42%253A29)%250A%2520%2520%2520%2520at%2520new%2520% 253Canonymous%253E%2520(HTTP%253A%252F%252Flocalhost%252Ftestauthenti%252Fnode_modules%252Frestangular%252Fdist%252Frestangular.js%253A812%253A16)%250A%2520%2520%2520%2520at%2520Object.instantiate%2520(HTTPS%253A %252F%252Fcdnjs.cloudflare.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 25% 3A41%253A370)%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%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%2520克%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%20原子%20https%3A%2F%2Fcdnjs.cloudflare .COM%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min.js%3A7%3A76%0A%20%20%20%20原子%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs %2Fangular.js%2F1.6.5%2Fangular.min.js%3A43 3A70%%0A%20%20%20%20原子%20P%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular的.js%2F1.6.5%2Fangular.min.js%3A8%3A7)%0A%20%20%20%20原子%20克%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular。 JS%2F1.6.5%2Fangular.min.js%3A42%3A138)%0A%20%20%20%20原子%20https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5 %2Fangular.min.js%3A42%3A322%0A%20%20%20%20原子%20P%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular .min.js%3A8%3A7)%0A%20%20%20%20原子%20克%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular。 min.js%3A42%3A138)%0A%20%20%20%20原子%20GB%20(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6.5%2Fangular.min的.js%3A46%3A251)%0A%20%20%20%20原子%20c的20%(HTTPS%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.6 0.5%2Fangular.min.js%3A22 3A19%)%0A%20%20%20%20原子%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/ $注射器/ unpr?P0 = RestangularProvider%20%3 C-%20Restangular%20%3 C-%20MainCtrl

code 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');
    });

请帮我解决此错误并感谢先进

1 个答案:

答案 0 :(得分:0)

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

http://underscorejs.org/#isUndefined

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