0

According to the Angular docs:

Scope is the glue between application controller and the view. During the template linking phase the directives set up $watch expressions on the scope. The $watch allows the directives to be notified of property changes, which allows the directive to render the updated value to the DOM.

Now my Question is : if my function is not connected to the view, should we use $scope or not?

1
  • 1
    I think you shouldn't. It will improve your app performance, because function not connected to scope will not be checked in each digest cycle. Commented Nov 6, 2014 at 7:57

2 Answers 2

2

I assume that you mean if you should do $scope.functionName = function(), even if the function isn't connected to the view.

No you shouldn't, why would you expose a function to the view, which isn't needed to the view? Also you get a better overview which functions is internally when only using function funcName().

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

2 Comments

correct, that is what I thought, but not using $scope, can make that function global. and it would be accessible from any where. right?
I dont think so. It is still in the context of the controller.
1

You shouldn't use the $scope to declare every function you are using, especially if it's not connected to the view.

However, there are some cases you need to use the $scope in a function not connected to view, for example if you want to emit/receive/broadcast a message on the scope tree or access something on a parent scope (although it's not necessarly a good practice).

1 Comment

If you want to emit/receive/brodcast you can use service and pass $scope as argument to some method from that service.

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.