简体   繁体   中英

$route depend on the current URL ($location)

in my angularJS app I use symfony as rest API.

Depending on my current url I'm using the $http under app_dev.php or not. I realized this with the following code:

app.run (function( $rootScope, $location ){

    $rootScope.dev = "";
    if ( $location.absUrl().search("app_dev.php") > 0 ) {
        $rootScope.dev = "app_dev.php/";
    }
}

app.controller('OfferIndexCtrl', function($scope, $http, $location, $filter, $rootScope){

    $http.get( $rootScope.dev + 'stage/offer/get').success(function(json){
        $scope.offerList = json.offerList;
    });
}

This works fine. But the .run() runs after .config() and its not possible to integrate it into the routeProvider.

Can anyone help me to integrate this into my routeProvider

app.config(['$routeProvider', '$locationProvider',
        function( $routeProvider, $locationProvider ){

            $routeProvider.
                when('/', {
                    templateUrl: 'stage/view/offer/index',
                    controller: 'OfferIndexCtrl'
                }).
                otherwise({
                    redirectTo: '/'
                })
        }
]);

You should use <base> tag to set base URL for your app, and set your base to "/" or "/app_dev.php/" depending on your Symfony environment, using TWIG helpers for environment. You can use this for "app_test.php" as well.

<base href="{{ path('homepage') }}">

With this your whole application will just work on that base URL, you don't need anything more.

How base HTML5 tag works with AngularJS application is described in detail here ngRoute set base url for all routes

If I'm using plain JS befor the Angular part I can use this variable from there. Following code worked for me:

var dev = "";
if ( location.pathname.search("app_dev.php") > 0 ) {
        dev = "app_dev.php/";
        console.log( "App läuft unter dev");
}

App.config(['$routeProvider', '$locationProvider',
    function( $routeProvider, $locationProvider ){

        $routeProvider.
            when('/', {
                templateUrl: dev + 'stage/view/offer/index',
                controller: 'OfferIndexCtrl'
            }).
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM