0

I am stuck here, I don't know what I am missing or how to debug this further. I continue to get this error: 'updateMemberServiceFactory is undefined' when I call it from an ng-click event. Please advise. If this is a simple typo I apologize I just can't see what's wrong. I'm trying to call into a PUT method on my controller but it never gets called. New to AngularJS. Thank you.

securityApp.factory('updateMemberServiceFactory', function ($http) {

    function update(memberServiceID) {

        $http({ method: 'PUT', url: 'http://localhost:62791/api/MemberServices/', data: { memberServiceID: memberServiceID } })
            .then(function (result) {
                alert('success');
            }, function (errorResponse) {

        });
    };

});

securityApp.controller('memberServicesController', function ($scope, $http, $routeParams, $location, getTokenFromServer, updateMemberServiceFactory) {

    var id = $routeParams.memberID;

    $scope.username = '[email protected]';
    $scope.password = 'SuperPass1!';

    getTokenFromServer.getToken($scope.username, $scope.password).then(function (data) {
        $scope.token = data;

        $http({ method: 'GET', url: '/api/MemberServices/' + id + '?access_token=' + $scope.token, headers: { 'Authorization': 'Bearer ' + $scope.token } })
            .success(function (response) {

                $scope.memberServices = "";
                $scope.memberServices = response;

                $http({ method: 'GET', url: '/api/Members/' + id + '?access_token=' + $scope.token, headers: { 'Authorization': 'Bearer ' + $scope.token } })
                    .success(function (response) {
                        $scope.member = response;
                    });

                $http.get('/api/ServiceTypes/')
                    .then(function (response) {
                        $scope.serviceTypes = response.data;
                    });
            });
    });

    $scope.updateMemberService = function () {        
        updateMemberServiceFactory.update( { memberServiceID: memberServiceID }, null, function () {
            alert('update called');
        });
    };

});

<a href="" style="text-decoration: none;" ng-click="updateMemberService(memberService.memberServiceID)"><i class="fa fa-save"></i></a>  
1

1 Answer 1

3

When you use someApp.factory(someFunction) the some someFunction should return an object that will be injected when needed.

In your case:

securityApp.factory('updateMemberServiceFactory', function ($http) {

    function update(memberServiceID) {

        $http({ method: 'PUT', url: 'http://localhost:62791/api/MemberServices/', data: { memberServiceID: memberServiceID } })
            .then(function (result) {
                alert('success');
            }, function (errorResponse) {

        });
    };

    return { // <---- this object will get injected, when required
         update : update
    }

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

Comments

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.