0

I'm trying to use a function of my directive to redirect when I click with a ng-click.

html :

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

Directive :

 (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");
            // }
            //


        }

})();

I got the error angular.js:12520 TypeError: Cannot set property 'href' of undefined

Tried $window.location.href and $location.path

Kalas Yagami
  • 183
  • 1
  • 20

3 Answers3

4

It didn't work, and I'm not patient. So I decided to make it easier :

I used window.location. It worked !

Kalas Yagami
  • 183
  • 1
  • 20
0

if your using angularjs use this $location.path("/home.html");

R.Anandan
  • 323
  • 5
  • 19
  • angular.js:12520 TypeError: $location.path is not a function function clickedEn(){ $location.path("/en/home.html"); } – Kalas Yagami Oct 21 '16 at 12:33
  • i think u need to add $location dependency in your controller for example "app.controller('loginCtrl', function($scope, $location, $window, $rootScope, $http, $q)" – R.Anandan Oct 21 '16 at 12:35
  • Already did it I think, or maybe I need to add it another place ? function NavBarLinksController($location, $window, CONTEXT, $timeout, loginService, openPageService, globalContextService, responsiveService ) – Kalas Yagami Oct 21 '16 at 12:36
  • Edited original post. – Kalas Yagami Oct 21 '16 at 12:39
  • you can add the $location at this line NavBarLinksController.$inject = ['CONTEXT', '$timeout', 'loginService', 'openPageService', 'globalContextService', 'responsiveService']; – R.Anandan Oct 21 '16 at 12:44
  • Did and : "angular.js:12520 TypeError: $location.path is not a function" – Kalas Yagami Oct 21 '16 at 12:48
  • refer this :http://stackoverflow.com/questions/19359553/angular-location-path-not-working – R.Anandan Oct 21 '16 at 12:50
0

If you use $stateProvider to build your route then you can do this like that :

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

Don't forget to inject $state

Bobo
  • 462
  • 8
  • 17