For example, this service:
services.factory('ElementsService', function () {
var currentElement = 'default element';
var service = {
getCurrentElement: function () {
return currentElement;
},
setCurrentElement: function (elmnt) {
currentElement = elmnt;
}
}
return service;
I often find it helpful to do the following from controllers:
controllers.controller('ElementsCtrl', function($scope, ElementsService) {
$scope.elementsService = ElementsService;
});
To be able to bind utility variables in html and stay up to date if the variables are changed by another controller or service. For instance:
<p>The current element is : {{elementsService.getCurrentElement()}}</p>
My question is: is this normal or should I avoid this?
source
share