I'm trying to route something using angular-route.js library, but I don't know how to do properly.
I would like to change a message when the browsers route change, but I get a lot of errors to do that.
index.html
<!doctype html>
<html ng-app="my-app">
<head>
<title>Angular controllers</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- AngularJS and JQuery include. -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular-route.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"></link>
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"></link>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<p> <a href="#ShowOrder/1556">Order 1556</a> </p>
<p> <a href="#ShowOrder/1667">Order 1667</a> </p>
</div>
<div ng-view></div>
<script src="app.js"></script>
</body>
</html>
app.js
(function (){
var app = angular.module('my-app', ['ngRoute']);
app.config (['$routeProvider', function ($routeProvider){
$routeProvider.when ('/ShowOrder/:orderId', {
templateUrl : 'show-order.html',
controller : 'ShowOrderCtrl'
});
}]);
app.controller ('ShowOrderCtrl', ['$routeParams', '$scope', function ($routeParams, $scope){
$scope.order = $routeParams.orderId;
}]);
})();
show-order.html
<h2>Order #{{order}}</h2>
Here are the details for order <b>#{{order}}</b>.
Error code
XMLHttpRequest cannot load file:///Users/ismaelmoral/GitHub/angularjs-controllers/show-order.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.(anonymous function) @ angular.js:10506sendReq @ angular.js:10325$get.serverRequest @ angular.js:10037processQueue @ angular.js:14551(anonymous function) @ angular.js:14567$get.Scope.$eval @ angular.js:15830$get.Scope.$digest @ angular.js:15641$get.Scope.$apply @ angular.js:15935bootstrapApply @ angular.js:1624invoke @ angular.js:4443doBootstrap @ angular.js:1622bootstrap @ angular.js:1642angularInit @ angular.js:1536(anonymous function) @ angular.js:28289x.Callbacks.l @ jquery.min.js:4x.Callbacks.c.fireWith @ jquery.min.js:4x.extend.ready @ jquery.min.js:4S @ jquery.min.js:4
angular.js:12314 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///Users/ismaelmoral/GitHub/angularjs-controllers/show-order.html'.
at Error (native)
at
localhost? I see that you are trying to load a view that triggers a cross origin error. Try setting up a server for yourlocalhostto get rid of the error, or, simply move the view's (html) location to the same location as your script.localhost. A simple server is for example serve. Once installed just runservein your project folder.