我试图让工作偏袒而不成功。
我在控制台中有错误,告诉我几件事:
1 / $ routeProvider表示未在todolist.js中定义,而ngRoute在模块中声明,而angular-route.js请求 2 /它在controller.js中说我错过了第4行(功能线)的正式参数
todolist.js
var todoList = angular.module('todoList',['ngRoute','todoListController']);
todoList.config([$routeProvider,function($routeProvider){
$routeProvider
.when('/inbox',{
templateUrl:'partials/inbox.html',
controller: 'todoCtrl'
})
.when('/today',{
templateUrl:'partials/today.html',
controller: 'todoCtrl'
})
.otherwhise({
redirectTo: '/inbox'
})
}])
controller.js
var todoListController= angular.module('todoListController', []);
todoListController.controller('todoCtrl',['$scope',
function ('$scope') {
var todos = $scope.todos = [];
$scope.addTodo = function () {
var newTodo = $scope.newTodo.trim();
if (!newTodo.length) {
return;
}
todos.push({
title: newTodo,
completed: false
});
$scope.newTodo = '';
};
$scope.removeTodo = function (todo) {
todos.splice(todos.indexOf(todo), 1);
};
$scope.markAll = function (completed) {
todos.forEach(function (todo) {
todo.completed = !completed;
});
};
$scope.clearCompletedTodos = function () {
$scope.todos = todos = todos.filter(function (todo) {
return !todo.completed;
});
};
}]);
的index.html
<!DOCTYPE html>
<html lang="fr" ng-app="todoList">
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid" >
<div class="navbar-header">
<a class="navbar-brand" href="#">Pense-bête</a>
</div>
<div class="navbar navbar-right" id="myNavbar" >
<ul class="nav navbar-nav">
<li><button type="button" class="btn btn-default navbar-btn" ng-click="clearCompletedTodos()">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button></li>
<li><button type="button" class="btn btn-default navbar-btn" ng-click="clearCompletedTodos()">
<span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
</button></li>
<li><button type="button" class="btn btn-default navbar-btn" ng-click="clearCompletedTodos()">
<span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span>
</button></li>
</ul>
</div>
</div>
</nav>
<div class="row">
</div>
<div class="row" id="menu">
<div class="col-sm-3 cold-md-2" id="left-menu">
<ul class="nav nav-pills nav-stacked">
<li class="active"><a href="#home" data-toggle="tab"><span class="glyphicon glyphicon-inbox">
</span> Boite de réception</a></li>
<li><a href="#"><span class="glyphicon glyphicon-calendar"></span> Aujourd'hui</a></li>
<li><a href="#"><span class="glyphicon glyphicon-calendar"></span> Cette semaine</a></li>
<li><a href="#"><i class="glyphicon glyphicon-pushpin"></i> Important </a></li>
<li><a href="#profile" data-toggle="tab"><span class="glyphicon glyphicon-cutlery"></span>
Repas</a></li>
<li><a href="#messages" data-toggle="tab"><span class="glyphicon glyphicon-shopping-cart"></span>
Courses</a></li>
<li><a href="#messages" data-toggle="tab"><span class="glyphicon glyphicon-list"></span>
Perso</a></li>
<li><a href="#messages" data-toggle="tab"><span class="glyphicon glyphicon-plus"></span>
Nouvelle liste</a></li>
</ul>
</div>
<div class="col-sm-9 cold-md-10" id='main-view' ng-controller="todoCtrl" >
<form id="todo-form" ng-submit="addTodo()">
<input id="new-todo" placeholder="Que voulez-vous noter ?" ng-model="newTodo" />
</form>
<div class="view" ng-view>
</div>
</div>
</div>
<script src="js/jquery.js"></script>
<script src="js/angular.js"></script>
<script src="js/angular-route.js"></script>
<script src="js/todolist.js"></script>
<script src="js/controller.js"></script>
</body>
</html>
inbox.html
<article ng-show="todos.length">
<ul id="todo-list">
<li ng-repeat="todo in todos" ng-class="{completed: todo.completed}">
<div class="view">
<input class="mark" type="checkbox" ng-model="todo.completed" />
<span>{{todo.title}}</span>
<span class="close" ng-click="removeTodo(todo)">x</span>
</div>
</li>
</ul>
<div>
<input id="mark-all" type="checkbox" ng-model="allChecked" ng-click="markAll(allChecked)" />
<label class="btn btn-info" for="mark-all">Tout cocher</label>
<button class="btn btn-danger" ng-click="clearCompletedTodos()">Supprimer les tâches cochées</button>
</div>
</article>
答案 0 :(得分:0)
当显式命名依赖项时(正如您所做),您需要首先将实际命名的依赖项作为字符串传递,然后将命名参数传递给函数。
todoList.config(['$routeProvider', function($routeProvider){}]);
此处$routeProvider
依赖项必须显式命名为字符串,然后作为第一个参数传递给函数。使用此方法可以实现代码缩小而不会中断,因为Angular不再依赖于将参数命名为精确依赖项(以及其他好处)。
与上面类似,你的第二个错误是因为你有一个字符串作为函数参数,它应该是一个变量名。
todoListController.controller('todoCtrl', ['$scope', function($scope){}]);
此外,您需要告诉您的Angular模块使用控制器,而不是创建一个新模块并将控制器置于其上。这样,您的todoList
模块就会知道todoListController
var todoListController= angular.module('todoListController', []);
应该成为
var todoListController= angular.module('todoList');