Desidero impostare il valore dell'attributo href
del tag base
in base a un valore costante. Per farlo ho dichiarato l'attributo base
nell'elemento head
come segue:È possibile impostare l'attributo href di un tag base in AngularJs?
<head>
<base ng-href="{{baseUrl}}">
</head>
e impostare il valore baseUrl
con questo frammento di codice:
app.run(["$rootScope","env", function($rootScope, env){
$rootScope.baseUrl = env.baseUrl;
}]);
dove env
è la costante angolare.
Il locationProvider
è configurato in questo modo:
.config(function ($locationProvider) {
$locationProvider.html5Mode(true);
})
quando l'eseguo vedo il valore impostato correttamente:
<base ng-href="/" href="/">
ma nella console ottengo questo errore:
Error: [$location:nobase] $location in HTML5 mode requires a <base> tag to be present!
http://errors.angularjs.org/1.3.14/$location/nobase
at REGEX_STRING_REGEXP (angular.js:63)
at $LocationProvider.$get (angular.js:11293)
at Object.invoke (angular.js:4185)
at angular.js:4003
at getService (angular.js:4144)
at Object.invoke (angular.js:4176)
at angular.js:4003
at getService (angular.js:4144)
at Object.invoke (angular.js:4176)
at angular.js:6485
Se imposto direttamente l'attributo href senza ngHref:
<base href="{{baseUrl}}">
ottengo questo errore:
Error: Failed to execute 'replaceState' on 'History': A history state object with URL 'http://items/' cannot be created in a document with origin 'http://localhost:9000'.
at Error (native)
at Browser.self.url (http://localhost:9000/bower_components/angular/angular.js:5044:56)
at setBrowserUrlWithFallback (http://localhost:9000/bower_components/angular/angular.js:11313:18)
at http://localhost:9000/bower_components/angular/angular.js:11435:15
at Scope.$get.Scope.$eval (http://localhost:9000/bower_components/angular/angular.js:14401:28)
at Scope.$get.Scope.$digest (http://localhost:9000/bower_components/angular/angular.js:14217:31)
at Scope.$get.Scope.$apply (http://localhost:9000/bower_components/angular/angular.js:14506:24)
at bootstrapApply (http://localhost:9000/bower_components/angular/angular.js:1448:15)
at Object.invoke (http://localhost:9000/bower_components/angular/angular.js:4185:17)
at doBootstrap (http://localhost:9000/bower_components/angular/angular.js:1446:14)
Dove items
è il percorso predefinito di routing:
.otherwise({
redirectTo: 'items'
});
Questo potrebbe essere perché la posizione html5 api. prova a mettere * $ locationProvider.html5Mode (true); * nella tua applicazione ** config ** function. –
Sto impostando html5Mode su true. – Fedy2
se si utilizza html5 posizione api invece hashbang angolare (! #), Non è necessario usare * ng-href *, basta usare ** href ** nel proprio elemento ** base **. –