我在this教程之后熟悉MEAN
堆栈。我开始使用ui-router
开始使用路由配置。我已经设置了所有内容,但我的页面并没有在<ui-view>
元素中呈现任何内容。
(这在添加路由配置之前有效)
这是我的文件的样子:
的index.html:
<!DOCTYPE html>
<html>
<head>
<title>Flapper News</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script scr="indexHomeState.js"></script>
<script src="indexController.js"></script>
<script src="postsService.js"></script>
<style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="flapperNews">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
<script type="text/ng-template" id='/home.html'>
<div class="page-header">
<h1>Flapper News</h1>
</div>
<div ng-repeat="post in indexCtrl.getPosts() | orderBy:'-upvotes'">
<span class="glyphicon glyphicon-thumbs-up"
ng-click="indexCtrl.incrementUpvotesForPost(post)"></span>
{{post.upvotes}}
<span style="font-size:20px; margin-left:10px;">
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
</span>
</div>
<form ng-submit="indexCtrl.addPost()"
style="margin-top:30px;">
<h3>Add a new post</h3>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Title"
ng-model="indexCtrl.title"></input>
</div>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Link"
ng-model="indexCtrl.link"></input>
</div>
<button type="submit" class="btn btn-primary">Post</button>
</form>
</script>
</body>
</html>
routingConfig(indexHomeState.js)
angular.module('flapperNews')
.config('indexHomeState', indexHomeState);
indexHomeState.$inject = ['$stateProvider', '$urlRouterProvider'];
function indexHomeState($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'indexController as indexCtrl'
});
$urlRouterProvider.otherwise('home');
}
我的角度app /控制器声明:
angular.module('flapperNews', ['ui.router'])
.controller('indexController', indexController);
indexController.$inject = ['postsService'];
function indexController(postsService) {
var vm = this;
vm.test = 'Hello world';
vm.getPosts = postsService.getPosts;
vm.addPost = postsService.addPost(vm.title, vm.link);
vm.incrementUpvotesForPost = postsService.incrementUpvotesForPost;
}
发布服务
angular.module('flapperNews')
.factory('postsService', postsService);
function postsService() {
var serv = this;
var posts = [
{title: 'post 1', upvotes: 5},
{title: 'post 2', upvotes: 2},
{title: 'post 3', upvotes: 15},
{title: 'post 4', upvotes: 9},
{title: 'post 5', upvotes: 4}
];
function getPosts() {
return posts;
}
function addPost(title, link) {
if(!title || title === '') { return; }
posts.push({
title: vm.title,
link: vm.link,
upvotes: 0
});
vm.title = '';
vm.link = '';
}
function incrementUpvotesForPost(post) {
post.upvotes ++;
}
return {
getPosts: getPosts,
addPost: addPost,
incrementUpvotesForPost: incrementUpvotesForPost
}
}
答案 0 :(得分:2)
您的代码看起来很好,只有您缺少的是,您需要更改
$urlRouterProvider.otherwise('home');
到
$urlRouterProvider.otherwise('/home');
此外,您src
scr
拼错了indexHomeState.js
,这限制了您的JS文件加载
<强>脚本强>
<script src="indexController.js"></script>
由于您的应用中没有indexHomeState
提供商,因此您不应将indexHomeState
添加为配置块中的字符串。
angular.module('flapperNews')
.config('indexHomeState', indexHomeState);
这只是
angular.module('flapperNews')
.config(indexHomeState); //removed `'indexHomeState',` from config