1

I have come across a quite peculiar error on my AngularJS code of a web application.

I have an application which has a form for inserting new data in the database, but before that it's going to look for any coincidences.

Now, the object that we insert in database has a series of collections and if it already exists the application will show the whole content on the UI, where the user will be able to add new items to the collections

The collections are made of entities which each have their own view and independent access to them from the navbar menu, so when I call the add new item method in the form it's actually recycled from the previous one, and here is the funny part

I have done three of them in the form and they all work fine, same view shows, same functionality, but when I try with the fourth one, this error comes up:

angular.js:13294 Error: [$injector:unpr] Unknown provider: pagingParamsProvider <- pagingParams <- TreatmentController2
http://errors.angularjs.org/1.5.2/$injector/unpr?p0=pagingParamsProvider%20%3C-%20pagingParams%20%3C-%20TreatmentController

Now, the even funnier thing is that I am using the same code for the controller and for the view as what is used on the separate entity view access, yet when I do it from the form it won't work.

If I remove the paging params injection, it loads the view, but of course, it loses all functionality.

Here's the code:

Controller:

  (function() {
    'use strict';

    angular
        .module('vivaxDataManagerApp')
        .controller('TreatmentController', TreatmentController);

    TreatmentController.$inject = ['$scope', '$state', 'Treatment', 'TreatmentSearch', 'ParseLinks', 'AlertService', 'pagingParams', 'paginationConstants'];

    function TreatmentController ($scope, $state, Treatment, TreatmentSearch, ParseLinks, AlertService, pagingParams, paginationConstants) {
        var vm = this;
        vm.loadAll = loadAll;
        vm.loadPage = loadPage;
        vm.predicate = pagingParams.predicate;
        vm.reverse = pagingParams.ascending;
        vm.transition = transition;
        vm.clear = clear;
        vm.search = search;
        vm.searchQuery = pagingParams.search;
        vm.currentSearch = pagingParams.search;
        vm.loadAll();



         function loadAll () {
                if (pagingParams.search) {
                    TreatmentSearch.query({
                        query: pagingParams.search,
                        page: pagingParams.page - 1,
                        size: paginationConstants.itemsPerPage,
                        sort: sort()
                    }, onSuccess, onError);
                } else {
                    Treatment.query({
                        page: pagingParams.page - 1,
                        size: paginationConstants.itemsPerPage,
                        sort: sort()
                    }, onSuccess, onError);
                }
                function sort() {
                    var result = [vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc')];
                    if (vm.predicate !== 'id') {
                        result.push('id');
                    }
                    return result;
                }
                function onSuccess(data, headers) {
                    vm.links = ParseLinks.parse(headers('link'));
                    vm.totalItems = headers('X-Total-Count');
                    vm.queryCount = vm.totalItems;
                    vm.treatments = data;
                    vm.page = pagingParams.page;
                }
                function onError(error) {
                    AlertService.error(error.data.message);
                }
            }

            function loadPage (page) {
                vm.page = page;
                vm.transition();
            }

            function transition () {
                $state.transitionTo($state.$current, {
                    page: vm.page,
                    sort: vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc'),
                    search: vm.currentSearch
                });
            }

            function search (searchQuery) {
                if (!searchQuery){
                    return vm.clear();
                }
                vm.links = null;
                vm.page = 1;
                vm.predicate = '_score';
                vm.reverse = false;
                vm.currentSearch = searchQuery;
                vm.transition();
            }

            function clear () {
                vm.links = null;
                vm.page = 1;
                vm.predicate = 'id';
                vm.reverse = true;
                vm.currentSearch = null;
                vm.transition();
            }

        }
    })();

The call from the HTML code:

<div class="form-group-row">
            <label class="col-md-2 form-control-label">Insert new Treatment:</label>
            <div class="col-md-2">
                <a class="btn btn-success" ng-click="newTreatment()">
                    <span class="glyphicon glyphicon-plus"/>
                </a>
            </div>
        </div>

The function in the form controller:

 $scope.newTreatment = function () {
                $scope.myHidingValue=true;
                $uibModal.open({
                    templateUrl: 'app/entities/treatment/treatment-dialog.html',
                    controller: 'TreatmentController',
                    size: 'lg',
                    controllerAs: 'vm',
                    backdrop: 'static',
                    resolve: {
                        entity: function () {
                            return {};
                        }
                    }
                }).result.then(function (result) {
                    $scope.publi = result;
                }, function () {
                })
            };

I'm only posting what I think is the most relevant code, as I have thousands of lines in this project, but if necessary, I don't mind posting more code

Thank you all in advance

Steven

Steven
  • 1,236
  • 1
  • 13
  • 37
  • It looks like `pagingParams` has not been registered with `$provide`. How is it defined? – The Head Rush May 10 '16 at 19:36
  • Hi, thank you for your answer, but I don't think I understand your question, the code is from the very same controller that works properly on a different section of the application, also I have three more controllers with same injections and they all work on this section of the app – Steven May 10 '16 at 19:42
  • You need to learn about dependency injection. The short version is you are trying to use an object that Angular knows nothing about, so Angular cannot inject the dependency into your controller function. What does the definition of `pagingParams` look like? – The Head Rush May 10 '16 at 20:11
  • I really appreciate the time and effort you put into answering to my post, but, as you say, I need to learn about dependency, as a matter of fact I need to learn about everything in Angular, therefore I don't understand what information you're trying to have me provide you with your question. As far as I know, I define pagineParams on this line: TreatmentController.$inject = ['$scope', '$state', 'Treatment', 'TreatmentSearch', 'ParseLinks', 'AlertService', 'pagingParams', 'paginationConstants']; Which I had posted earlier on in the controllers code, other than that I am lost – Steven May 10 '16 at 21:01
  • That's where the `pagingParameters` reference is injected into the controller you wrote, not where the value is defined. What is `pagingParameters` exactly? A variable? – The Head Rush May 10 '16 at 23:41
  • Hi again, thank you for explaining, I get it now :) Ok, pagingParams injection is automatically generated by the Yeoman JHipster project generator when you create an entity, I just gathered it was a dependency that belonged to Angular, but after reading your question and researching I haven't found any documentation for it I can now see where the mistake takes shape, yet I can't undertand why it doesn't recognize the variable in this particular case but it does in others which have exactly the same code. – Steven May 11 '16 at 08:49
  • I'm glad you git it sorted out. Happy coding! – The Head Rush May 11 '16 at 13:10

1 Answers1

0

Mistery solved, the problem was that I wasn't using the right controller, where I should of had TreatmentDialogController, I had TreatmentController, shame on me, I know.

Thank you to The Head Rush for leading me in the right direction!

Steven
  • 1,236
  • 1
  • 13
  • 37