0

Link 1 Link 2

I am trying to get current URL in alert but I guess angular script is running first before alert, so when I click link 2 its showing link 1 and vice versa. Is there anything i am missing or is there a to get URL of the page after page is loaded in angularjs 1.7

(function () {

    var app = angular.module('myApp');

    app.controller('initiativesController', ['$scope',
      function ($scope) {
          $scope.TestInitiatives = "Test";

          var url = window.location.pathname.split('/')[1];;
          alert(url)

          console.log($scope.TestInitiatives);
      }
    ]);
})();

Angular app.js

(function () {

    var myapp = angular.module('myApp', ["ui.router"]);

    myapp.config(function ($stateProvider, $locationProvider, $urlRouterProvider) {

        // For any unmatched url, send to /route1
        $urlRouterProvider.otherwise("/route1")

        $stateProvider
          .state('route1', {
              url: "/route1",
              templateUrl: "SOC/Views/route1.html",
              controller: "route1ctrl"
          })
          .state('route2', {
              url: "/route2",
              templateUrl: "SOC/Views/route2.html",
              controller: "route2ctrl"
          })

        $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });

    });


})();
sgl
  • 563
  • 1
  • 6
  • 16
  • updated question with ui-router – sgl Nov 15 '18 at 16:02
  • you are using `html5Mode`. I bet most of your problems are happening because of it. Did you change your server-side routing manually? Because you will be getting `404` errors and have problems getting the right URL – Aleksey Solovey Nov 15 '18 at 16:04
  • no i never tried that – sgl Nov 15 '18 at 16:06
  • if i access `http://localhost:56631/route1` it gives 404 but if I `ui-sref="route1"` on any link or button it works – sgl Nov 15 '18 at 16:08
  • if I remove I get link with # `http://localhost:56631/#!/route1` which i dont want that – sgl Nov 15 '18 at 16:10
  • Is there any way to remove `#!` – sgl Nov 15 '18 at 16:11
  • I already told you that's what `html5mode` is for, but you need to fix the routing on **server-side** – Aleksey Solovey Nov 15 '18 at 16:12
  • Do you have any sample code how that can be handled on server side – sgl Nov 15 '18 at 16:14
  • for a `.htaccess` file (**Apache** Web Server configuration file) you might want to try [this solution](https://stackoverflow.com/questions/22739455/htaccess-redirect-for-angular-routes/22740184#22740184) – Aleksey Solovey Nov 15 '18 at 16:17
  • so i need to create this script file named `Test.htaccess` and refer that file on Index.html masterpage? – sgl Nov 15 '18 at 16:21
  • no, that file is literally called `.htaccess` (and nothing else). Because it starts with a `.`, it might be hidden in your directory. Post that code inside of it and test for any changes on your (local) server. You shouldn't experience 404 errors – Aleksey Solovey Nov 15 '18 at 16:24
  • thanks but my question is `.htaccess` is JS file? Do i need to copy paste that code in somewhere or where that .htaccess file should be placed? – sgl Nov 15 '18 at 16:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183736/discussion-between-aleksey-solovey-and-sgl). – Aleksey Solovey Nov 15 '18 at 16:29

0 Answers0