I read this article:
http://teropa.info/blog/2014/10/24/how-ive-improved-my-angular-apps-by-banning-ng-controller.html
What suggests integrating controllers into such directives in order to remove the need to use an ng controller:
angular.module('contestantEditor', []) .directive('cContestantEditorForm', function() { return { scope: { contestants: '=' }, templateUrl: 'contestant_editor.html', replace: true, controller: 'ContestantEditorFormCtrl', controllerAs: 'ctrl', bindToController: true }; }) .controller('ContestantEditorFormCtrl', function($scope) { this.contestant = {}; this.save = function() { this.contestants.push(this.contestant); this.contestant = {}; }; });
In the comments, however, someone else suggested this solution:
angular.module('contestantEditor', []) .directive('cContestantEditorForm', function () { return { scope: { contestants: '=' }, templateUrl: 'contestant_editor.html', replace: true, link: function (scope) { scope.contestant = {}; scope.save = function() { scope.contestants.push(scope.contestant); scope.contestant = {}; }; } }; });
It does the same thing as the controller version, without even requiring the creation of a controller. So I'm curious what the pros and cons of any method are compared to writing angular traditionally with an ng controller, and whether controllers are needed by the end.
Here is the plunker for the first, and here is the second.
source share