AngularJS指令$ location.path()返回空字符串

时间:2015-04-13 10:02:23

标签: javascript angularjs

希望有人可以对此有所了解:

我有一个简单的指令,$location.host()& $location.protocol()总是返回一个空字符串。

MyDirective.js

'use strict';

angular.module('KeirSimpleApp')
.directive('ims', ['$rootScope', '$window', '$log', '$location', function($rootScope, $window, $log, $location) {
    var url = '';
    if($location.path().indexOf('qe') > -1) {
        //do something
        url = $location.host();
    }



    return {
        template: '<script type="text/javascript" src="http://' + $location.host() + '"></script>',

        restrict: 'E',

        link: function() {
            $rootScope.userState = {
                isUserSignedIn: false
            };
        }
    }
}]);

App.js:

'use strict';
angular
  .module('ersUiApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap'
  ])
    .config(function ($routeProvider, $locationProvider) {
        $routeProvider
            .when('/index.html', {
                templateUrl: 'components/main/main.html',
                controller: 'MainCtrl'
            })
            .when('/preview', {
                templateUrl: 'components/preview/preview.html',
                controller: 'PreviewCtrl'
            });

        $locationProvider.html5Mode({
          enabled: true
        }).hashPrefix();
    })  
    .constant('_', window._)
    .run(function ($rootScope, $location) {
     $rootScope._ = window._;
  });

如果我需要更详细,请告诉我。任何帮助将不胜感激!

0 个答案:

没有答案