0

I am following https://github.com/StarterSquad/startersquad.github.com/tree/master/examples/angularjs-requirejs-2 folder structure in my app

inside services i have added following code

 define(['./module'], function(services) {
'use strict';
services.factory('CreditCard', ['$http', function($http) {

function CreditCardFactory() {

    function parseMessage(message) {
        if (message.response) {
            return message.response;
        }
    }

    function CreditCard(value) {
        angular.copy(value || {}, this);
    }

    CreditCard.$get = function(id) {
        var value = this instanceof CreditCard ? this : new CreditCard();
        $http({
            method: 'GET',
            url: '/creditcards/' + id
        }).then(function(response) {
            var data = response.data;
            if (data) {
                angular.copy(parseMessage(data), value);
            }
        });
        return value;
    };

    CreditCard.prototype.$get = function(id) {
        CreditCard.$get.call(this, id);
    };

    return CreditCard;

  }

return CreditCardFactory;

}]);


});

i have followed this question and added above code in factory

Angularjs - customizing $resource

in this question app.controller('CreditCardCtrl', function($scope, CreditCard) { $scope.creditCard = CreditCard().get(3); }); CreditCard is added without adding dependency as we add defualt angular services like $scope and $http.

if i dont add dependency controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources',function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){ then it gives undefined and if i add it in my controller as dependency and then try to call get function then response is not returned

     controllers.controller('myListCtrl',    ['Phone','Phone1','loginForm','$scope','$http','CreditCard',function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){

1 Answer 1

1

'CreditCard' isn't declared as a dependency in this example, therefore it is not being injected and is undefined...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources',
    function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){

To fix this, add 'CreditCard'...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources','CreditCard',
    function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){

In the second example, some dependencies are missing, causing the others to be in the wrong order in comparison to the function arguments...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','CreditCard',
    function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){

Add the missing dependencies in the right order...

controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','userSrv','$scope','$http','user_resources','CreditCard',
    function(Phone,Phone1,loginForm,userSrv,$scope,$http,user_resources,CreditCard){
Sign up to request clarification or add additional context in comments.

1 Comment

after declaring dependency as controllers.controller('myListCtrl', ['Phone','Phone1','loginForm','$scope','$http','user_resources','CreditCard', function(Phone,Phone1,loginForm,$scope,$http,user_resources,CreditCard){ console.log(CreditCard); is undefined and console.log(CreditCard()) throws error undefined is not a function at Scope.$scope.loadMyLeaveList

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.