Skip to content

Commit

Permalink
Use ControllerAs pattern in angularjs archetype
Browse files Browse the repository at this point in the history
  • Loading branch information
jogep committed Aug 22, 2015
1 parent 16fa0ed commit 3bd1b7a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<a href="/home">Home</a> - <a href="/projects">Projects</a>
</div>

<div ng-controller="AppController">
<div ng-controller="AppController as app">
<div ng-view></div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

$routeProvider.when('/projects', {
templateUrl: 'partials/projects.html',
controller: 'ApacheProjectsController'
controller: 'ApacheProjectsController as vm'
}).when('/home', {
templateUrl: 'partials/home.html',
controller: 'HomeController'
controller: 'HomeController as vm'
}).otherwise({ redirectTo: '/home' });
}
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -25,15 +23,18 @@
.module('app')
.controller('ApacheProjectsController', ApacheProjectsController);

function ApacheProjectsController($scope, $log, DataService) {
this.init = function() {
DataService.getProjects().then(function(data) {
$scope.projects = data.projectNames;
function ApacheProjectsController($log, DataService) {
var vm = this;

init();

function init() {
return DataService.getProjects().then(function(data) {
vm.projects = data.projectNames;
return vm.projects;
}, function() {
$log.error('Could not receive project names.');
});
};

this.init();
}
}
})();
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -22,8 +20,8 @@
'use strict';

angular
.module('app')
.controller('AppController', AppController);
.module('app')
.controller('AppController', AppController);

function AppController() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -25,7 +23,8 @@
.module('app')
.controller('HomeController', HomeController);

function HomeController($scope) {
$scope.name = "Sunshine";
function HomeController() {
var vm = this;
vm.name = "Sunshine";
}
})();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>Hello, {{name}}.</h1>
<h1>Hello, {{vm.name}}.</h1>

<p>
Not your name?
<input type="text" ng-model="name" />
<input type="text" ng-model="vm.name" />
</p>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<ul>
<li ng-repeat="project in projects">{{project}}</li>
<li ng-repeat="project in vm.projects">{{project}}</li>
</ul>

0 comments on commit 3bd1b7a

Please sign in to comment.