Lets say I have the following controller
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function ($scope) {
$scope.username = 'World';
$scope.sayHello = function () {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}]);
is there any reason why i should not use object literals
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function ($scope) {
$scope.viewModel = {
greeting: '',
username: 'World',
sayHello: function(){
this.greeting = 'Hello ' + this.username + '!';
}
};
}]);
I personally prefer to use object literal rather than bind everything $scope. Its manageable and good practice.
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function ($scope) {
var viewModel = {
greeting: '',
username: 'World'
};
viewModel.sayHello = function () {
this.greeting = 'Hello ' + this.username + '!';
};
$scope.viewModel = viewModel;
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With