2

Inside an angular.js controller we could add methods to the scope or to the controller

this.controllerFoo = function(){
};

or

$scope.scopeFoo = function(){
}

When to use what? Currently i add only methods to scope when they are needed inside the view. This feels like good design for me.

but are there any further decissions to make when deciding what to add the method?

1 Answer 1

2

the better practice is to actually use the the "controller as" syntax (the this.myFunction approach), because it makes your html clearer and shows you exactly which controller you're using when you're invoking a method (it makes the most sense when you have nested controllers).

take a look at Todd Motto's post, it's very informative

Sign up to request clarification or add additional context in comments.

1 Comment

John Papa's styleguide is also very helpfull github.com/johnpapa/angularjs-styleguide 'controller as' binds to $scope but prevents $parent issues when nesting controllers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.