0

I am trying to create an Angular service that gets images and passes those images to controller to push to a carousel. I did all thework on my controller and it works now with a service, it doesnt read the service function. My index.html file

<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script>
    <script src="example.js"></script>
    <script src="example-service.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="CarouselDemoCtrl">
 <div id="fl-main" class="container">
        <div class="col-xs-6 col-md-4 filter">
            <label class="btn btn-primary" ng-model="male" ng-click="loadhome(male)">Male</label>
      <label class="btn btn-primary" ng-model="female">Female</label>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-8">
            <div carousel interval="myInterval">
        <div slide ng-repeat="slide in slides" active="slide.active">
            <img ng-src="{{slide.image}}" style="margin:auto; width: ">
            <div class="carousel-caption">      
            </div>
        </div>
        </div>
        </div>
    </div>
</div>
  </body>
</html>

My controller file, example.js

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('CarouselDemoCtrl', function ($scope, homeService) {

  $scope.loadhome = function(gender) {  
        $scope.myInterval = 5000;
        var slides = $scope.slides = [];
        var items = homeService.loadhomepage(gender);

    for (var i=0; i<items.length; i++)
            {
                slides.push({   
                image: items[i].image
      });
    }
    };

});

my service file. example-service.js

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').factory('homeService', function () {

    var loadhomepage = function(gender) {
        console.log('inloadhomepage');
        var slides = [];
        if(gender==='male')
        {
        slides.items =[
         {src:'images/carousel.jpg'},
         {src:'images/carousel1.jpg'}
        ];
    }
    return slides;
    };

});

So basically when i click the button male, it should call the loadhome() function in controller which shud assign the items in the service function to var items in the controller. Then controller will push all images to $scope.slides. here is my plunkr file. http://plnkr.co/edit/JcHrHDJkgFxDfSVcqIJZ

how do i get it to return with items length to be equal to the array it receives? enter image description here

0

3 Answers 3

2

You have setup factory correctly as you have only defined loadhomepage as local function, you need to expose these methods. and you don't need to redefine module again so use angular.module('ui.bootstrap.demo', ['ui.bootstrap']); only once.

Change it to:

angular.module('ui.bootstrap.demo')
    .factory('homeService', function() {
        return {
            loadhomepage: function(gender) {
                console.log('inloadhomepage');
                var slides = [];
                if (gender === 'male') {
                    slides.items = [{
                        src: 'images/carousel.jpg'
                    }, {
                        src: 'images/carousel1.jpg'
                    }];
                }
                return slides;
            }
        }
    });

DEMO

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

5 Comments

@KingsleySimon, Whats not working? only thing I can see is var slides = [];intstead use var slides = {};
It isnt going into the forEach function plus it caches the value and never deletes it. Even when i change the values. It has stored in somewhere and keeps loading the same data.
when i console.log(items) in d controller, it returns array [2] but when i console length it returns 0. hence i cant push it to slides in the controller. how do i solve this?
also i changed 'src' to 'image' in d service file and it still returns src not changing any value in the service why?
see my revised code in my controller file and the image as well, it doesnt go into the for loop because it is returning the length of the array to be 0. while inside the object there r 2 items.
1

This is what worked for me. None of the answers worked for me here. In my service function i did however had to change to var slides = {}; and in my controller file

 angular.module('fastLaneApp').controller('HomeCtrl', function ($scope, homeService) {

        $scope.loadhome = function(gender) {  
            $scope.myInterval = 5000;
                var slides = $scope.slides = [];
              var items = homeService.loadhomepage(gender);

             angular.forEach(items, function (item, index) {
                console.log(index);
                console.log(item);
                for (var i=0; i<item.length; i++)
                        {
                    slides.push({   
                     image: item[i].src
                  });
                }
                });
            };
    });

Comments

0

In your case loadhomepage is a local variable which exists only in an anonymous function.

You need to return the service interface from the service definition

angular.module('ui.bootstrap.demo').factory('homeService', function () {
    var loadhomepage = function (gender) {
        console.log('inloadhomepage');
        var slides = [];
        if (gender === 'male') {
            slides.items = [{
                src: 'images/carousel.jpg'
            }, {
                src: 'images/carousel1.jpg'
            }];
        }
        return slides;
    };
    return {
        loadhomepage: loadhomepage
    }
});

Demo: Fiddle

3 Comments

doesnt work @Arun. I saw your fiddle. my service and controller files are separate. could tht b a reason?
@KingsleySimon yes it is because of that... see jsfiddle.net/arunpjohny/keqop26k/1 - not working(the module definition is repeated), jsfiddle.net/arunpjohny/keqop26k/3 - working
see my revised code in my controller file and the image as well, it doesnt go into the for loop because it is returning the length of the array to be 0. while inside the object there r 2 items.

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.