Use ControllerAs pattern in angularjs archetype

This commit is contained in:
Johannes Geppert
2015-08-22 12:38:51 +02:00
parent 16fa0ed80c
commit 3bd1b7a723
7 changed files with 21 additions and 23 deletions
@@ -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>
@@ -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' });
}
]);
@@ -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
@@ -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();
}
}
})();
@@ -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
@@ -22,8 +20,8 @@
'use strict';
angular
.module('app')
.controller('AppController', AppController);
.module('app')
.controller('AppController', AppController);
function AppController() {
@@ -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
@@ -25,7 +23,8 @@
.module('app')
.controller('HomeController', HomeController);
function HomeController($scope) {
$scope.name = "Sunshine";
function HomeController() {
var vm = this;
vm.name = "Sunshine";
}
})();
@@ -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>
@@ -1,3 +1,3 @@
<ul>
<li ng-repeat="project in projects">{{project}}</li>
<li ng-repeat="project in vm.projects">{{project}}</li>
</ul>