Is it correct to access scope inside service or factory in angularjs
Disclaimer : I know there are certain questions which suggest not to access scope inside service or factory but here I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.
We have angular js project and this project is old. Now after refactoring one of my colleague moved the common implementation from directive to service. While doing so , to access the scope of directive he manually started doing as below :
angular.element('<test-dir></test-dir>').scope();
What I felt is this is not the proper way to write the service/factory. I felt we are making the things complicated and suggested to remove the above part of code.
To justify the same I told :
1. This will make unit testability complicated and now we are trying to test the service the way we used to test directive.
2. And we are making this service tightly coupled with directive.
3. Service is not meant to access the scope.
But I think I am not able to convince him as I don't have much point to justify it. Can someone please suggest if I my understanding is correct and give proper justification to convice him. Thanks!
angularjs
add a comment |
Disclaimer : I know there are certain questions which suggest not to access scope inside service or factory but here I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.
We have angular js project and this project is old. Now after refactoring one of my colleague moved the common implementation from directive to service. While doing so , to access the scope of directive he manually started doing as below :
angular.element('<test-dir></test-dir>').scope();
What I felt is this is not the proper way to write the service/factory. I felt we are making the things complicated and suggested to remove the above part of code.
To justify the same I told :
1. This will make unit testability complicated and now we are trying to test the service the way we used to test directive.
2. And we are making this service tightly coupled with directive.
3. Service is not meant to access the scope.
But I think I am not able to convince him as I don't have much point to justify it. Can someone please suggest if I my understanding is correct and give proper justification to convice him. Thanks!
angularjs
Theelement.scope()method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of$rootScope. Services however can inject$rootScopeand access the scope methods such as$broadcast,$on,$apply, etc.
– georgeawg
Nov 23 '18 at 15:20
The example above does not make sense. An element that is created from a string has no scope. The.scope()method will returnundefined.
– georgeawg
Nov 23 '18 at 15:36
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06
add a comment |
Disclaimer : I know there are certain questions which suggest not to access scope inside service or factory but here I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.
We have angular js project and this project is old. Now after refactoring one of my colleague moved the common implementation from directive to service. While doing so , to access the scope of directive he manually started doing as below :
angular.element('<test-dir></test-dir>').scope();
What I felt is this is not the proper way to write the service/factory. I felt we are making the things complicated and suggested to remove the above part of code.
To justify the same I told :
1. This will make unit testability complicated and now we are trying to test the service the way we used to test directive.
2. And we are making this service tightly coupled with directive.
3. Service is not meant to access the scope.
But I think I am not able to convince him as I don't have much point to justify it. Can someone please suggest if I my understanding is correct and give proper justification to convice him. Thanks!
angularjs
Disclaimer : I know there are certain questions which suggest not to access scope inside service or factory but here I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.
We have angular js project and this project is old. Now after refactoring one of my colleague moved the common implementation from directive to service. While doing so , to access the scope of directive he manually started doing as below :
angular.element('<test-dir></test-dir>').scope();
What I felt is this is not the proper way to write the service/factory. I felt we are making the things complicated and suggested to remove the above part of code.
To justify the same I told :
1. This will make unit testability complicated and now we are trying to test the service the way we used to test directive.
2. And we are making this service tightly coupled with directive.
3. Service is not meant to access the scope.
But I think I am not able to convince him as I don't have much point to justify it. Can someone please suggest if I my understanding is correct and give proper justification to convice him. Thanks!
angularjs
angularjs
asked Nov 23 '18 at 13:32
devdev
1217
1217
Theelement.scope()method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of$rootScope. Services however can inject$rootScopeand access the scope methods such as$broadcast,$on,$apply, etc.
– georgeawg
Nov 23 '18 at 15:20
The example above does not make sense. An element that is created from a string has no scope. The.scope()method will returnundefined.
– georgeawg
Nov 23 '18 at 15:36
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06
add a comment |
Theelement.scope()method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of$rootScope. Services however can inject$rootScopeand access the scope methods such as$broadcast,$on,$apply, etc.
– georgeawg
Nov 23 '18 at 15:20
The example above does not make sense. An element that is created from a string has no scope. The.scope()method will returnundefined.
– georgeawg
Nov 23 '18 at 15:36
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06
The
element.scope() method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of $rootScope. Services however can inject $rootScope and access the scope methods such as $broadcast, $on, $apply, etc.– georgeawg
Nov 23 '18 at 15:20
The
element.scope() method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of $rootScope. Services however can inject $rootScope and access the scope methods such as $broadcast, $on, $apply, etc.– georgeawg
Nov 23 '18 at 15:20
The example above does not make sense. An element that is created from a string has no scope. The
.scope() method will return undefined.– georgeawg
Nov 23 '18 at 15:36
The example above does not make sense. An element that is created from a string has no scope. The
.scope() method will return undefined.– georgeawg
Nov 23 '18 at 15:36
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06
add a comment |
1 Answer
1
active
oldest
votes
No, the services/factory are supposed to be working with data and should have the logic to process the data provided to them. Preferably, they should not refer to DOM objects or scope variables.
I personally believe that passing $scope to a service is a bad idea, because it creates a kinda circular reference: the controller depends on the service and the service depends on the scope of the controller.
On top of being confusing in terms of relations, things like this one end up getting in the way of the garbage collector.
A Service is just a function for the business layer of the application. It acts as a constructor function and is invoked once at runtime with new, much like you would with plain JavaScript (Angular is just calling a new instance under the hood for us).Use a
servicewhen you want to just create things that act as public APIs.
The service class should work on the data provided by the controller, making it reusable at any other place if needed. Also keeping the scope away from it, keeps the code lean and clean thus better maintainability.
I prefer to put a domain object in the controller scope and pass that to the service. This way the service works irrespective of it being used inside a controller or maybe inside another service in the future.
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :$scope.person = {'name:'xyz, age: 35, gender:'male'}You can just use this person in controller and pass it to service or any of it's attribute when needed.
– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447668%2fis-it-correct-to-access-scope-inside-service-or-factory-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
No, the services/factory are supposed to be working with data and should have the logic to process the data provided to them. Preferably, they should not refer to DOM objects or scope variables.
I personally believe that passing $scope to a service is a bad idea, because it creates a kinda circular reference: the controller depends on the service and the service depends on the scope of the controller.
On top of being confusing in terms of relations, things like this one end up getting in the way of the garbage collector.
A Service is just a function for the business layer of the application. It acts as a constructor function and is invoked once at runtime with new, much like you would with plain JavaScript (Angular is just calling a new instance under the hood for us).Use a
servicewhen you want to just create things that act as public APIs.
The service class should work on the data provided by the controller, making it reusable at any other place if needed. Also keeping the scope away from it, keeps the code lean and clean thus better maintainability.
I prefer to put a domain object in the controller scope and pass that to the service. This way the service works irrespective of it being used inside a controller or maybe inside another service in the future.
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :$scope.person = {'name:'xyz, age: 35, gender:'male'}You can just use this person in controller and pass it to service or any of it's attribute when needed.
– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
add a comment |
No, the services/factory are supposed to be working with data and should have the logic to process the data provided to them. Preferably, they should not refer to DOM objects or scope variables.
I personally believe that passing $scope to a service is a bad idea, because it creates a kinda circular reference: the controller depends on the service and the service depends on the scope of the controller.
On top of being confusing in terms of relations, things like this one end up getting in the way of the garbage collector.
A Service is just a function for the business layer of the application. It acts as a constructor function and is invoked once at runtime with new, much like you would with plain JavaScript (Angular is just calling a new instance under the hood for us).Use a
servicewhen you want to just create things that act as public APIs.
The service class should work on the data provided by the controller, making it reusable at any other place if needed. Also keeping the scope away from it, keeps the code lean and clean thus better maintainability.
I prefer to put a domain object in the controller scope and pass that to the service. This way the service works irrespective of it being used inside a controller or maybe inside another service in the future.
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :$scope.person = {'name:'xyz, age: 35, gender:'male'}You can just use this person in controller and pass it to service or any of it's attribute when needed.
– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
add a comment |
No, the services/factory are supposed to be working with data and should have the logic to process the data provided to them. Preferably, they should not refer to DOM objects or scope variables.
I personally believe that passing $scope to a service is a bad idea, because it creates a kinda circular reference: the controller depends on the service and the service depends on the scope of the controller.
On top of being confusing in terms of relations, things like this one end up getting in the way of the garbage collector.
A Service is just a function for the business layer of the application. It acts as a constructor function and is invoked once at runtime with new, much like you would with plain JavaScript (Angular is just calling a new instance under the hood for us).Use a
servicewhen you want to just create things that act as public APIs.
The service class should work on the data provided by the controller, making it reusable at any other place if needed. Also keeping the scope away from it, keeps the code lean and clean thus better maintainability.
I prefer to put a domain object in the controller scope and pass that to the service. This way the service works irrespective of it being used inside a controller or maybe inside another service in the future.
No, the services/factory are supposed to be working with data and should have the logic to process the data provided to them. Preferably, they should not refer to DOM objects or scope variables.
I personally believe that passing $scope to a service is a bad idea, because it creates a kinda circular reference: the controller depends on the service and the service depends on the scope of the controller.
On top of being confusing in terms of relations, things like this one end up getting in the way of the garbage collector.
A Service is just a function for the business layer of the application. It acts as a constructor function and is invoked once at runtime with new, much like you would with plain JavaScript (Angular is just calling a new instance under the hood for us).Use a
servicewhen you want to just create things that act as public APIs.
The service class should work on the data provided by the controller, making it reusable at any other place if needed. Also keeping the scope away from it, keeps the code lean and clean thus better maintainability.
I prefer to put a domain object in the controller scope and pass that to the service. This way the service works irrespective of it being used inside a controller or maybe inside another service in the future.
answered Nov 23 '18 at 16:22
nircraftnircraft
1,465419
1,465419
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :$scope.person = {'name:'xyz, age: 35, gender:'male'}You can just use this person in controller and pass it to service or any of it's attribute when needed.
– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
add a comment |
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :$scope.person = {'name:'xyz, age: 35, gender:'male'}You can just use this person in controller and pass it to service or any of it's attribute when needed.
– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
Please include a link to the source that you are quoting.
– georgeawg
Nov 23 '18 at 16:50
domain object means any object with bunch of properties attached , Ex :
$scope.person = {'name:'xyz, age: 35, gender:'male'} You can just use this person in controller and pass it to service or any of it's attribute when needed.– nircraft
Nov 23 '18 at 20:58
domain object means any object with bunch of properties attached , Ex :
$scope.person = {'name:'xyz, age: 35, gender:'male'} You can just use this person in controller and pass it to service or any of it's attribute when needed.– nircraft
Nov 23 '18 at 20:58
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
I would call that a JavaScript object.
– georgeawg
Nov 23 '18 at 21:09
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
It can be that or A domain object is an entity in the domain layer of your application, eg. an Address class. If you use Typescript or ES6 based classes, you can have such object classes to represent your object.
– nircraft
Nov 23 '18 at 21:28
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
To clarify, we are not passing $scope object but we are manually getting the scope of directive using angular.element and then scope() inside a service. Thanks.
– dev
Nov 25 '18 at 6:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447668%2fis-it-correct-to-access-scope-inside-service-or-factory-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The
element.scope()method Requires Debug Data to be enabled. This prevents the significant performance boost that can be gained by turning it off. It is not wise to use that method in production code. Services are singletons and should not modify scopes which are hierarchical extensions of$rootScope. Services however can inject$rootScopeand access the scope methods such as$broadcast,$on,$apply, etc.– georgeawg
Nov 23 '18 at 15:20
The example above does not make sense. An element that is created from a string has no scope. The
.scope()method will returnundefined.– georgeawg
Nov 23 '18 at 15:36
I am voting to close this question as off-topic as answers will be primarily based on opinions..
– georgeawg
Nov 23 '18 at 16:57
Absolutely, the answer will be based on opinion, thats why I had added disclaimer stating "I am expecting the impact in terms of coding guidelines / whether it is advisable if not then I need proper justification.(Added now : in technical perspecive)"
– dev
Nov 25 '18 at 6:03
@georgeawg And just to clarify inside angular.element we have a custom directive, so we are taking the directive and then getting its scope object and its working as well . And even my understanding is this is not the correct approach but as mentioned I need proper justification to prove this. Thank you .
– dev
Nov 25 '18 at 6:06