0

I am trying to call the update / put method in the factory which will in turn save the changes on the form to the database via an API call. But I get a console error below. The update function is getting called from the button click fine, but it doesn't call the factory and API from there. What am I missing? Thank you!

I updated my code with the suggestion below but now have this error:

My console error: "Error: [$injector:unpr] http://errors.angularjs.org/1.2.10/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20memberUpdate


var securityApp = angular.module('securityApp', ['ngRoute']). config(function ($routeProvider) {

$routeProvider

    .when('/', {
        templateUrl: 'PartialPages/members.html',
        controller: 'membersController'
    })

    .when('/memberDetail/:memberID', {
        templateUrl: 'PartialPages/memberDetail.html',
        controller: 'memberDetailController'
    })

    .when('/memberEdit', {
        templateUrl: 'PartialPages/memberEdit.html',
        controller: 'memberEditController'
    });

});


securityApp.factory('memberUpdate', function ($resource) {       
    return $resource('/api/Members/:id', { id: '@id' }, { update: { method: 'PUT' } });
});


securityApp.controller('memberDetailController', function ($scope, $http, $routeParams, memberUpdate) {

    var id = $routeParams.memberID;

    $http.get('/api/Members/' + $routeParams.memberID).success(function (data) {
        $scope.member = data;
    })
    .error(function () {
        $scope.error = "An Error has occured while loading posts!";
    })

    $scope.update = function () {
        memberUpdate.update({ id: id }, $scope.member);
    };

});

2 Answers 2

3

You need to inject memberUpdate into the controller dependencies.

securityApp.controller('memberDetailController', function ($scope, $http, $routeParams, memberUpdate) {

    var id = $routeParams.memberID;

    $http.get('/api/Members/' + $routeParams.memberID).success(function (data) {
        $scope.member = data;
    })
    .error(function () {
        $scope.error = "An Error has occured while loading posts!";
    })

    $scope.update = function () { // you don't need to pass $scope and memberUpdate since they are already available into the scope
        memberUpdate.update({ id: id }, $scope.member);
    };

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

1 Comment

I get this error when I try your suggestions: "Error: [$injector:unpr] errors.angularjs.org/1.2.10/$injector/…
1
$resource is in a different module  so you need to include it.
var securityApp = angular.module('securityApp', ['ngRoute', 'ngResource']).

her how you install it https://docs.angularjs.org/api/ngResource

3 Comments

Thank you. I added the angular resource script and that solved the problem.
the correct syntax is: var securityApp = angular.module('securityApp', ['ngRoute', 'ngResource']).
@Austin Harris yes sorry for the mistake

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.