As far as I know there are two ways to work with needed methods:
1) add methods to $scope and call them in ng-xxx (for instance, in ng-click)
2) call controller methods directly in ng-xxx (for instance, 
ng-click="myController.someMethod()" )

Prototypes usage allows to define methods in several files but do not 
change the code which uses them (i.e. ng-click="myController.someMethod()" 
will be leaved as is)
As I understand services usage makes to put similar methods to $scope (i.e. 
to use 1) and controller/prototype usage (i.e. 2) is not possible.

Am I right?

P.S. Indeed I may define similar method in each controller and to call 
service from these controllers' methods but such way look too bad.

On Thursday, October 30, 2014 11:37:50 AM UTC+2, Maryan Bahnyuk wrote:
>
> Thank you for suggestion, Jens
>
> As I understand:
> 1) each service should extend $scope of controller by adding the 
> appropriate methods - there is no need to use JavaScript prototypes at all 
> - all methods should be added to $scope
> 2) it is a 'good style' if one service adds several methods to $scope (I 
> mean that there is no need to create a separate service for each similar 
> method)
>
> Please correct me if I'm wrong.
>
> On Wednesday, October 1, 2014 5:46:51 PM UTC+3, Jens Melgaard wrote:
>>
>> Use Composite Reuse Principle instead: Create a Service that does the 
>> work, share that service. 
>>
>> On Wednesday, October 1, 2014 12:07:27 PM UTC+2, Maryan Bahnyuk wrote:
>>>
>>> Dear Witold
>>>
>>> On Wednesday, October 1, 2014 10:40:55 AM UTC+3, Witold Szczerba wrote:
>>>
>>>> From OO perspective, it is a bad idea to use inheritance for code reuse 
>>>>
>>> IMHO it's much better then just copying the same code from one 
>>> controller to another. And inheritance in this case allows not to duplicate 
>>> code in two controllers (moreover in 2+ controllers).
>>>
>>>
>>> On Wednesday, October 1, 2014 10:40:55 AM UTC+3, Witold Szczerba wrote:
>>>
>>>> Also, it is a static linking, so you cannot mock/replace those methods 
>>>> in a straightforward way if you need it.
>>>>
>>> Sorry for possible misunderstanding but I can mock/replace those methods 
>>> by changing them on-fly or by making 'hooks'... From another point of view 
>>> I do not need this and it's a subject of another discussion :)
>>>
>>>
>>> On Wednesday, October 1, 2014 10:40:55 AM UTC+3, Witold Szczerba wrote:
>>>
>>>> Sometimes it might be pragmatic to use inheritance, so there is no 
>>>> clear "STOP" sign. 
>>>>
>>> IMHO it's the best way to fix my 'issue' of duplicating code. The main 
>>> question is "Does AngularJS have any restrictions regarding such usage or 
>>> not?".
>>>
>>>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to