2

I have a service that is injected in my controllers. The service defines a number of functions.

Now I would like to add a variable to that service that would hold the selectedItem in the application. I've done it this way:

angular.module('myservices', []).
factory('serviceA', function () {        

    var serviceA= {  
      selectedItem: selectedItem,      
      ... more functions here
    };
    return serviceA;

    var selectedItem;
    ... functions go here
});

In one of my controllers I set the selected item:

 serviceA.selectedItem = someItem;

and in another controller the view references the selected item like this:

<span>{{serviceA.selectedItem.value}}</span>

The span is never updated, even though the selectedItem is set correctly. What am I doing wrong ?

2
  • I forked this fiddle a few time ago to work on communication between controllers : jsfiddle.net/patxy/RAVFM (sorry, I don't remember who did it first). This fiddle uses an event to do it ($scope.broadcast). I suggest you to keep the service, and fire an event into this service Commented Aug 28, 2013 at 8:16
  • I want to avoid using broadcast. In fact, services in angularjs are singleton and that's why I want to use it for shared variable across controllers. I've only used broadcast and emit for directives Commented Aug 28, 2013 at 8:34

3 Answers 3

7

To access the service in your view, you need to assign it to the scope:

$scope.serviceA = serviceA;

http://jsfiddle.net/MzJsZ/

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

2 Comments

This is nice. I've also updated the demo to check myself. jsfiddle.net/MzJsZ/1
I've posted my own answer before you. But I'm going to accept yours. Thansk for helping !
1
angular.module('myservices', []).factory('serviceA', function () {        
  return {

//// factory returns an object

     var serviceA= {  
      selectedItem: selectedItem,      
      ... more functions here
    };
    return serviceA;

    var selectedItem;
    ... functions go here

}

});

does this work? you were missing the return{} part,where everything goes there. if you would use service you would write just this.serviceA={...}

and your code in controller is missing $scope !

$scope.someItem= serviceA.selectedItem; 

or (i dont really know what you want) $scope.serviceA.selectedItem= someItem;

then in your span

<span>{{someItem.value}}</span>

or (i dont really know what you want) <span>{{serviceA.selectedItem.value}}</span>

PS i dont like that your controllers name is the same as object's - serviceA;

2 Comments

I never do that, is it required ? My code works fine without it, I can call functions like ´serviceA.someFunction()´ from controllers. It's just for the selectedItem variable that I have an issue.
thanks for the edit. see my answer as it is the service itself that I needed to add to the scope. This way, I can bind the view directly to the service's variable, which is shared across controllers, i.e: if one controller modifies that value, the view belonging to another controller will automatically show changes. Don't worry about names, I've changed them all on SO but in my real code they are named differently. Thanks for helping!
0

Ah ! I got it to work !

Doing :

<span>{{serviceA.selectedItem.value}}</span>

in the view wouldn't work because although the serviceA is injected in the view's controller, I did not assign it to the scope... So correct way is to do that in the controller:

angular.module('controllers').controller('someCtrl',
['$scope', 'directDebitService', function ($scope, serviceA) {
    $scope.serviceA= serviceA;
}]);

Works fine now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.