Hi,

I'm building a relatively large application using angular and we're 
currently doing planning and testing.  One thing we're undecided on is 
controllerAs or classic $scope when writing controllers.  We're going to us 
a Service Model Locator so every single controller will have some injected 
dependencies.

I've built out a sample controller using both methods - I guess my 
questions are
1. what is the right/better approach
2. Is there another way to inject using controllerAs
3. Is there a performance difference and a good way to test this?

thanks in advance

Greg

Classic $scope method

angular.module('sdt.settingsctrl', [])
    .controller('SettingsCtrl', ['$scope','$log', 
'ModalService','SettingsModel', function ($scope, $log, ModalService, 
SettingsModel) {
        $scope.data = SettingsModel.getData();
        $scope.settings = SettingsModel.settings()

        $scope.open = function(){
            var items = [{item:'item1'},{item: 'item2'},{item: 'item3'}];
            var modalOptions = {
                closeButtonText: 'Cancel',
                actionButtonText: 'OK',
                headerText: 'Adjust your settings',
                bodyText: items,
                result: $scope.callBack(),

                //optional - if you use this option feel free to
                //add more properties to work with your custom template
                template: 'partials/modals/settings.html'
            }


            ModalService.showModal({}, modalOptions).then(function (result) {
                $log.log(result);
            });
        }

        $scope.callBack = function() {
            return SettingsModel.settings();
        }
    }])




ControllerAs method

angular.module('sdt.settingsctrl', [])
 .controller('SettingsCtrl', SettingsCtrl);

SettingsCtrl.$inject= ['$log', 'ModalService', 'SettingsModel'];

function SettingsCtrl($log, ModalService, SettingsModel){
    angular.extend(this, {
        data: SettingsModel.getData(),
        settings: SettingsModel.settings(),
        open:  function() {
            var items = [{item: 'item1'}, {item: 'item2'}, {item: 'item3'}];
            var modalOptions = {
                closeButtonText: 'Cancel',
                actionButtonText: 'OK',
                headerText: 'Adjust your settings',
                bodyText: items,
                result: this.callBack(),

                //optional - if you use this option feel free to
                //add more properties to work with your custom template
                template: 'partials/modals/settings.html'
            }


            ModalService.showModal({}, modalOptions).then(function (result) {
                $log.log(result);
            })
        },
        callBack: function() {
            return SettingsModel.settings();
        }
    })

};



-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to