【问题标题】:$window.location.href (angular.js:12520 TypeError: Cannot set property 'href' of undefined)$window.location.href (angular.js:12520 TypeError: Cannot set property 'href' of undefined)
【发布时间】:2016-10-21 12:29:45
【问题描述】:

当我通过 ng-click 进行点击时,我正在尝试使用我的指令的功能进行重定向。

html:

<a ng-click="navbarlinksCtrl.clickedfr()" ng-class="{active: clickedfr()}">FR</a><br>
<a ng-click="navbarlinksCtrl.clickeden()">EN</a>

指令:

 (function($) {
    'use strict';

    angular
        .module('eeeee')
        .directive('navBarLinks', navBarLinks);

    navBarLinks.$inject = ['CONTEXT'];

    /**
     * Directive which shows the navigation bar links
     * @memberof eeeee
     * @ngdoc directive
     * @name navBarLinks
     * @restrict E
     * @example
     * <nav-bar-links><div>Content of navigation bar links</div></nav-bar-links>
     */
    function navBarLinks(CONTEXT) {
        var directive = {
            bindToController: {
                activeLanguagesAsLocales: '@',
                current: '@',
                lang: '@',
                userPreferredLanguage: '@',
                siteActiveLanguages: '@'
            },
            controller: NavBarLinksController,
            controllerAs: 'navbarlinksCtrl',
            restrict: 'E',
            scope: {},
            templateUrl: CONTEXT.contextPath + '/modules/reeeeeetemplate/javascript/components/navBarLinks/navBarLinks.tpl.html'
        };

        return directive;
    }

    NavBarLinksController.$inject = ['CONTEXT', '$timeout', 'loginService', 'openPageService', 'globalContextService', 'responsiveService'];

        /**
         * Controller which shows the login header
         * @memberof eeeeeeeeeeee
         * @name navBarLinksController
         * @ngdoc controller
         */
    function NavBarLinksController($location, $window, CONTEXT, $timeout, loginService, openPageService, globalContextService, responsiveService ) {
            var vm = this;

            vm.clickedfr=clickedFr;
            vm.clickeden=clickedEn;


            // vm.isOpen = false;
            // vm.username = '';
            // vm.password = '';
            // vm.forgotPassword = forgotPassword;
            // vm.isOpen = false;
            // vm.submit = submit;
            // vm.resetError = resetError;
            // vm.isResponsive = false;
            // vm.updateResponsive = updateResponsive;

            activate();

            /////////////

            function activate() {
                // updateResponsive();
                // addStringEndsWithFunctionIfNotExist();
                // openLoginOnTheHomePage();
            }

            function clickedFr(){
                 // http://127.0.0.1:8081/fr/home.html
                 // openPageService.sendMessageOpenPage(CONTEXT.contextPath + "eeeee/home.html");
                $window.location.href = "/eeeeee/home";
            }

            function clickedEn(){
                $location.path("/en/home.html");

            }

            //
            // /**
            //  * Redirect to the password reset page
            //  * @memberof LoginController
            //  * @function forgotPassword
            //  */
            // function forgotPassword() {
            //     openPageService.sendMessageOpenPage(CONTEXT.contextPath + vm.forgotPasswordPath, "openLogin=false");
            // }
            //


        }

})();

我收到错误 angular.js:12520 TypeError: Cannot set property 'href' of undefined

试过 $window.location.href 和 $location.path

【问题讨论】:

    标签: javascript angularjs redirect angularjs-directive


    【解决方案1】:

    它没有用,我没有耐心。所以我决定让它变得更容易:

    我使用了 window.location。有效 !

    【讨论】:

      【解决方案2】:

      如果你使用 angularjs 使用这个 $location.path("/home.html");

      【讨论】:

      • angular.js:12520 TypeError: $location.path is not a function function clickedEn(){ $location.path("/en/home.html"); }
      • 我认为您需要在控制器中添加 $location 依赖项,例如“app.controller('loginCtrl', function($scope, $location, $window, $rootScope, $http, $q )"
      • 我想已经这样做了,或者我需要将它添加到另一个地方?函数 NavBarLinksController($location, $window, CONTEXT, $timeout, loginService, openPageService, globalContextService, responsiveService)
      • 已编辑原帖。
      • 你可以在这一行添加 $location NavBarLinksController.$inject = ['CONTEXT', '$timeout', 'loginService', 'openPageService', 'globalContextService', 'responsiveService'];
      【解决方案3】:

      如果您使用$stateProvider 来构建您的路线,那么您可以这样做:

      $state.go("/fr/home");
      

      别忘了注入$state

      【讨论】:

      • 我不使用它:/
      猜你喜欢
      • 2018-12-16
      • 2020-03-22
      • 2018-01-26
      • 1970-01-01
      • 2021-06-25
      • 2019-11-26
      • 1970-01-01
      • 2017-04-24
      • 2023-03-15
      相关资源
      最近更新 更多