1

What is the best way of sharing data between controllers using services in AngularJS?

For example, when the user selects an item from the <ion-list> in services.html, I'd like the title of the selected item to be displayed in service.html. {{service.name}} is some pseudocode I've written down to kinda spell out what I am trying to achieve.

services.html

    <ion-list>
        <ion-item ng-repeat="service in services" href="#/app/services/{{service.id}}" class="item-icon-right">
            {{service.title}} 
            <i class="icon ion-chevron-right icon-accessory">
                <span class="badge badge-positive">{{service.total}}</span>
            </i>
        </ion-item>
    </ion-list>

service.html

<ion-view view-title="Playlist">
    <ion-content>
        <h1>{{service.name}}</h1>
    </ion-content>
</ion-view>

controller.js

.controller('ServicesCtrl', ["$scope", "ServicesData", function($scope, ServicesData) {
  $scope.services = ServicesData.GetServices()
}])

.controller('ServiceCtrl', function($scope, $stateParams) {
});

services.js

angular.module('starter.services', [])

.service("ServicesData", [function () {

    var servicesData = [
        { 
            title: 'Car Repair and Maintenance', 
            total: 7, 
            id: 1 
        },
        { 
            title: 'Cleaning', 
            total: 1, 
            id: 2 
        },
        { 
            title: 'Delivery, Storage and Moving', 
            total: 6, 
            id: 3 
        }
    ];

    return {
        GetServices: function () {
            return servicesData;
        },
        GetServiceById: function () {
            // do stuff here to get the service by id
        }
    }
}])
2
  • 1
    just add method getServiceById to ServicesData and inject ServicesData to ServiceCtrl -- you get what you want. Commented Jan 18, 2016 at 0:43
  • Thanks for the reply. Can you please show me a code sample? Commented Jan 18, 2016 at 0:45

1 Answer 1

2

angular.module('starter.services', [])

.service("ServicesData", [function () {

    var servicesData = [
        { 
            title: 'Car Repair and Maintenance', 
            total: 7, 
            id: 1 
        },
        { 
            title: 'Cleaning', 
            total: 1, 
            id: 2 
        },
        { 
            title: 'Delivery, Storage and Moving', 
            total: 6, 
            id: 3 
        }
    ];

    return {
        getSelected: function(serviceId) {
          var selectedService;
          
          servicesData.forEach(function(service) {
            if(service.id === serviceId) {
              selectedService = service;
            }
          });
          
          return return selectedService;
        },
        getServices: function () {
            return servicesData;
        }
    }
}])

.controller('ServicesCtrl', ["$scope", "ServicesData", function($scope, ServicesData) {
  $scope.services = ServicesData.setServices();
}])

.controller('ServiceCtrl', function($scope, $stateParams) {
  $scope.service = ServicesData.getSelected($stateParams.service);//the param name should be defined in the route config
});
<!--services.html-->

    <ion-list>
        <ion-item ng-repeat="service in services" href="#/app/services/{{service.id}}" class="item-icon-right">
            {{service.title}} 
            <i class="icon ion-chevron-right icon-accessory">
                <span class="badge badge-positive">{{service.total}}</span>
            </i>
        </ion-item>
    </ion-list>

<!--service.html-->

<ion-view view-title="Playlist">
    <ion-content>
        <h1>{{service.name}}</h1>
    </ion-content>
</ion-view>

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

4 Comments

Thanks. Is there anything I need to add to app.js? This is what I have so far: pastebin.com/PZphbX44
@methuselah No, I guessed the param name correctly. It should work now
Thanks. I've tried it, but now get the following error: ReferenceError: ServicesData is not defined.
Ok I fixed it by updating ServiceCtrl to .controller('ServiceCtrl', function($scope, ServicesData, $stateParams) { $scope.service = ServicesData.getSelected($stateParams.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.