0

I'm trying to display 1 of the 2 arrays, this is my factory:

App.factory('NieuwsService', ['BERKVENS_NIEUWS', 'SVEDEX_NIEUWS', function (BERKVENS_NIEUWS, SVEDEX_NIEUWS) {
    var nieuwsService = {};

    var _nieuws = [];

    var _loadNieuws = function (url) {
        switch (url) {
        case 'berkvens':
            _nieuws = BERKVENS_NIEUWS;
            break;
        case 'svedex':
            _nieuws = SVEDEX_NIEUWS;
            break;
        }
    }

    nieuwsService.loadNieuws = _loadNieuws;
    nieuwsService.nieuws = _nieuws;

    return nieuwsService;

}]);

This is the code of my controller:

App.controller('instellingenCtrl', function ($scope, NieuwsService) {
    var url = 'berkvens';

    angular.copy(NieuwsService.loadNieuws(url));

    $scope.message= NieuwsService.nieuws[1].title;
});

But it wouldn't work.

So how do I choose that I want the array 'berkvens' and how do I show it then ?

jsfiddle

5
  • Where and how does 'BERKVENS_NIEUWS' and 'SVEDEX_NIEUWS' are defined? A fiddle with what you tried so far could help. Commented Sep 14, 2015 at 12:05
  • it would be better if you added BERKVENS_NIEUWS value in your questions. Commented Sep 14, 2015 at 12:16
  • What do you mean ? @gauravbhavsar Commented Sep 14, 2015 at 12:22
  • I edited the post, there is a fiddle now. @Quad Commented Sep 14, 2015 at 12:23
  • @Goldenowner I added working plunker in my answer, and its working fine. Commented Sep 14, 2015 at 12:26

1 Answer 1

1

You not need nieuwsService.nieuws = _nieuws; in your service.
You just need to return _nieuws after case : condition.

Working Plunker

Factory

App.factory('NieuwsService', ['BERKVENS_NIEUWS', 'SVEDEX_NIEUWS', function (BERKVENS_NIEUWS, SVEDEX_NIEUWS) {
    var nieuwsService = {};

    var _nieuws = [];

    var _loadNieuws = function (url) {
        switch (url) {
        case 'berkvens':
            _nieuws = BERKVENS_NIEUWS;
            return _nieuws; // return _nieuws if case 'berkvens'
        case 'svedex':
            _nieuws = SVEDEX_NIEUWS;
            return _nieuws; // return _nieuws if case 'svedex'
        }
    }

    nieuwsService.loadNieuws = _loadNieuws;

    return nieuwsService;

}]);

Controller

App.controller('instellingenCtrl', function ($scope, NieuwsService) {
    var url = 'berkvens';

    // copy loadNieuws  
    var copyLoadNieuws = angular.copy(NieuwsService.loadNieuws(url));

    $scope.message= copyLoadNieuws[1].title;
});
Sign up to request clarification or add additional context in comments.

2 Comments

can you use console.log(copyLoadNieuws) to check what you getting ? error is saying copyLoadNieuws is undefined in your controller.
check Plunker, I added in answer

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.