Luc*_*ges 10 angularjs angularjs-routing
我正在尝试运行此代码:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>Exemple 06</title>
</head>
<body>
<!-- Diretiva ng-repeat -->
<div ng-controller="MyController">
<a ng-href="#/">Page 1</a> | <a ng-href="#/p2">Page 2</a>
<div ng-view="#/p2"></div>
</div>
<script src="js/angular.js"></script>
<script src="js/angular-route.js"></script>
<script type="text/javascript">
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider){
$routeProvider
.when('/' , {controller: 'MyController', templateUrl: 'p1.html'})
.when('/p2', {controller: 'MyController', templateUrl: 'p2.html'})
.otherwise({ redirectTo: '/'});
});
myApp.controller('MyController', function($scope){
$scope.nomes = [
{id: 1, nome: 'Arthur'},
{id: 2, nome: 'Portos'},
{id: 3, nome: 'Aramis'}
];
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
但是会出现以下错误:
XMLHttpRequest无法加载file:///home/93579551515/Desktop/Angular/p1.html.仅支持HTTP的跨源请求.
我不想在网络服务器上运行它.
mus*_*_ut 30
您可以添加模板,如p1.html和p2.html你index.html通过将它们内部的script标签,如在文档中提到.
<script type="text/ng-template" id="p1.html">
This is the content of the template
</script>
Run Code Online (Sandbox Code Playgroud)
然后angular将不再需要发出AJAX请求来获取它们.
请注意,这些script元素必须出现在之后 ng-app.
| 归档时间: |
|
| 查看次数: |
30469 次 |
| 最近记录: |