简体   繁体   English

$route 取决于当前的 URL ($location)

[英]$route depend on the current URL ($location)

in my angularJS app I use symfony as rest API.在我的 angularJS 应用程序中,我使用 symfony 作为 rest API。

Depending on my current url I'm using the $http under app_dev.php or not.根据我当前的 url,我是否使用 app_dev.php 下的 $http。 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.但是 .run() 在 .config() 之后运行,并且不可能将它集成到 routeProvider 中。

Can anyone help me to integrate this into my routeProvider谁能帮我把它集成到我的 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.您应该使用<base>标记为您的应用程序设置基 URL,并根据您的 Symfony 环境将基地址设置为“/”或“/app_dev.php/”,使用环境的 TWIG 助手。 You can use this for "app_test.php" as well.您也可以将其用于“app_test.php”。

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

With this your whole application will just work on that base URL, you don't need anything more.有了这个,您的整个应用程序将只在该基本 URL 上运行,您不需要更多内容。

How base HTML5 tag works with AngularJS application is described in detail here ngRoute set base url for all routes此处详细描述了基本 HTML5 标记如何与 AngularJS 应用程序一起使用ngRoute set base url for all routes

If I'm using plain JS befor the Angular part I can use this variable from there.如果我在 Angular 部分之前使用纯 JS,我可以从那里使用这个变量。 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'
            }).
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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