I am trying to use some of the best methods defined on google-styleguide: https://google-styleguide.googlecode.com/svn/trunk/angularjs-google-style.html
But at the moment I'm struggling with some problems. Before I used this style, I had a $scope variable available for $watch for a variable, for example.
app.controller('myController',['$scope', function($scope) { $scope.$watch('myVariable', function(val) { alert("I'm changed"); }); }]);
Now, with my new approach, I don’t know how to handle this? Should I still enter $scope ? Because I do not need to enter $scope when I do not use $watch .
function myController($scope) { var vm = this; vm.myVariable = "aVariable"; vm.$watch('vm.myVariable', function(val) {
Prototype is also recommended for styleoid. But what if I had to introduce a service? What is the best approach to using the service inside your prototype?
function myController(MyService) { var vm = this; vm.myService = MyService; } myController.prototype.callService = function() { var vm = this; vm.myService.doSomething(); }
It is right? Or am I missing something, is there a place where I can find more information about this angular programming style?
In my opinion, this is more like natural javascript, and I want to use this way of organizing my AngularJS applications.
Thank you in advance
Update
For the “service” problem, I thought of something like the following:
function MyBaseController(AService, BService, CService) { this.aService = AService; this.bService = BService; this.cService = CService; } function myController() { var vm = this; MyBaseController.apply(vm, arguments); } myController.prototype.doSomething() { var vm = this; this.aService.somethingElse(); }
But that does not seem right imo ..