Commit 723b0059 authored by Pascal Hartig's avatar Pascal Hartig

AngularJS: Utilize ngRoute for filters

Supersedes #733

Inline-templating love contributed by @stephenplusplus <3
parent 51172b18
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
"name": "todomvc-angular", "name": "todomvc-angular",
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"angular": "~1.2.1", "angular": "1.2.5",
"todomvc-common": "~0.1.4" "todomvc-common": "~0.1.4"
}, },
"devDependencies": { "devDependencies": {
"angular-mocks": "~1.2.1" "angular-mocks": "1.2.5",
"angular-route": "1.2.5"
} }
} }
<!doctype html> <!doctype html>
<html lang="en" ng-app="todomvc" data-framework="angularjs"> <html lang="en" data-framework="angularjs">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
...@@ -7,7 +7,10 @@ ...@@ -7,7 +7,10 @@
<link rel="stylesheet" href="bower_components/todomvc-common/base.css"> <link rel="stylesheet" href="bower_components/todomvc-common/base.css">
<style>[ng-cloak] { display: none; }</style> <style>[ng-cloak] { display: none; }</style>
</head> </head>
<body> <body ng-app="todomvc">
<ng-view />
<script type="text/ng-template" id="todomvc-index.html">
<section id="todoapp" ng-controller="TodoCtrl"> <section id="todoapp" ng-controller="TodoCtrl">
<header id="header"> <header id="header">
<h1>todos</h1> <h1>todos</h1>
...@@ -37,13 +40,13 @@ ...@@ -37,13 +40,13 @@
</span> </span>
<ul id="filters"> <ul id="filters">
<li> <li>
<a ng-class="{selected: location.path() == '/'} " href="#/">All</a> <a ng-class="{selected: status == ''} " href="#/">All</a>
</li> </li>
<li> <li>
<a ng-class="{selected: location.path() == '/active'}" href="#/active">Active</a> <a ng-class="{selected: status == 'active'}" href="#/active">Active</a>
</li> </li>
<li> <li>
<a ng-class="{selected: location.path() == '/completed'}" href="#/completed">Completed</a> <a ng-class="{selected: status == 'completed'}" href="#/completed">Completed</a>
</li> </li>
</ul> </ul>
<button id="clear-completed" ng-click="clearCompletedTodos()" ng-show="completedCount">Clear completed ({{completedCount}})</button> <button id="clear-completed" ng-click="clearCompletedTodos()" ng-show="completedCount">Clear completed ({{completedCount}})</button>
...@@ -59,8 +62,10 @@ ...@@ -59,8 +62,10 @@
</p> </p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p> <p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer> </footer>
</script>
<script src="bower_components/todomvc-common/base.js"></script> <script src="bower_components/todomvc-common/base.js"></script>
<script src="bower_components/angular/angular.js"></script> <script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="js/app.js"></script> <script src="js/app.js"></script>
<script src="js/controllers/todoCtrl.js"></script> <script src="js/controllers/todoCtrl.js"></script>
<script src="js/services/todoStorage.js"></script> <script src="js/services/todoStorage.js"></script>
......
...@@ -7,4 +7,15 @@ ...@@ -7,4 +7,15 @@
* *
* @type {angular.Module} * @type {angular.Module}
*/ */
var todomvc = angular.module('todomvc', []); var todomvc = angular.module('todomvc', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider.when('/', {
controller: 'TodoCtrl',
templateUrl: 'todomvc-index.html'
}).when('/:status', {
controller: 'TodoCtrl',
templateUrl: 'todomvc-index.html'
}).otherwise({
redirectTo: '/'
});
});
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* - retrieves and persists the model via the todoStorage service * - retrieves and persists the model via the todoStorage service
* - exposes the model to the template and provides event handlers * - exposes the model to the template and provides event handlers
*/ */
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, filterFilter) { todomvc.controller('TodoCtrl', function TodoCtrl($scope, $routeParams, todoStorage, filterFilter) {
var todos = $scope.todos = todoStorage.get(); var todos = $scope.todos = todoStorage.get();
$scope.newTodo = ''; $scope.newTodo = '';
...@@ -21,15 +21,12 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage, ...@@ -21,15 +21,12 @@ todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage,
} }
}, true); }, true);
if ($location.path() === '') { // Monitor the current route for changes and adjust the filter accordingly.
$location.path('/'); $scope.$on('$routeChangeSuccess', function () {
} var status = $scope.status = $routeParams.status || '';
$scope.location = $location;
$scope.$watch('location.path()', function (path) { $scope.statusFilter = (status === 'active') ?
$scope.statusFilter = (path === '/active') ? { completed: false } : (status === 'completed') ?
{ completed: false } : (path === '/completed') ?
{ completed: true } : null; { completed: true } : null;
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment