【发布时间】:2016-05-06 18:58:50
【问题描述】:
我正在关注 here 的 angularui 教程。但是好像事情不配合,我只是得到一个空白的ui-view。
目录结构:
routes-app/
index.html
app.js
routes.js
home/
home.html
homeController.js
github/
github.html
githubController.js
gallery/
gallery.html
galleryController.js
文件:
<!--index.html-->
<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</head>
<body>
<h1>UI Router Tut</h1>
<div ui-view></div>
<!--{{2+3}}-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.min.js"></script>
<script src="app.js"></script>
<script src="routes.js"></script>
<script src="home/homeController.js"></script>
<script src="github/githubController.js"></script>
<script src="gallery/galleryController.js"></script>
</body>
</html>
//app.js
var myApp = angular.module('myApp',['ui.router']);
//routes.js
myApp.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider,$urlRouterProvider)
{
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: 'home/home.html',
controller: 'homeCtrl'
})
.state('gallery', {
url: '/gallery',
templateUrl: 'gallery/gallery.html',
controller: 'galleryCtrl'
})
.state('github', {
url: '/github',
templateUrl: 'github/github.html',
controller: 'githubCtrl'
});
]);
<!--home.html-->
<h2>Hello {{title}}</h2>
<button ng-click="ChangeState("github")">Go to github</button>
<button ui-sref="gallery">Check out gallery</button>
//homeController.js
myApp.controller('homeCtrl', ['$state', '$scope',
function ($state, $scope){
$scope.title = "Homey";
$scope.ChangeState = function (stateName){
$state.go(stateName);
};
}]
);
编辑:
控制台错误信息:
angular.js:11756 XMLHttpRequest cannot load file:///D:/.../Projects/angularapps/routes-app/home/home.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
【问题讨论】:
-
你试过 $location.path("/stateName");在 $scope.ChangeState() 中。
-
问题是我什至无法测试
$scope.ChangeState(),因为即使主页网址指定为'/',<div ui-view></div>中也没有显示任何内容。 -
您是否尝试过将
angular-ui-router脚本标签移动到<head>,在angular之后?根据您的浏览器,它可能在加载ui-viewdiv 之前尚未加载它,因此不知道如何处理它。 -
你在 app.js 中写过 .run(function($ionicPlatform) {} 吗??
-
@N.Raval 除非我遗漏了什么,否则 OP 似乎没有使用任何 Ionic 组件?
标签: angularjs angular-ui-router