Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Controller is not getting loaded/called #1606

Closed
o1lab opened this issue Oct 31, 2016 · 5 comments
Closed

Controller is not getting loaded/called #1606

o1lab opened this issue Oct 31, 2016 · 5 comments

Comments

@o1lab
Copy link

o1lab commented Oct 31, 2016

Im using meanjs for a webapp and have below issue.

When transitioning from state parents.view to parents.view.childs - I do not see the controller associated with parents.view.childs being called/loaded. But prints within getChilds() is seen for which I see server responding with childs data.

But I cannot understand why childs controllers is not getting called. There are no errors on console.

Is there something wrong with using controllerAs : 'vm' in both states or something else ?

Also what is the best way to debug state transitions in angular ?

angular route file

.state('parents', {
  abstract: true,
  url: '/parents',
   template: '<ui-view/>'
})
.state('parents.view', {
  url: '/:parentId',
  templateUrl: 'modules/parents/client/views/view-parent.client.view.html',
  controller: 'parentsController',
  controllerAs: 'vm',
  resolve: {
    parentResolve: getparent
  },
  data: {
    pageTitle: 'parent {{ parentResolve.name }}'
  }
})
.state('parents.view.childs', {
  url: '/childs',
  templateUrl: 'modules/childs/client/views/list-childs.client.view.html',
  controller: 'childsListController',
  controllerAs: 'vm',
  resolve: {
    childResolve: getchilds
  },
  data: {
    pageTitle: 'childs List'
  }
})

getchilds.$inject = ['$stateParams', '_childsService'];

function getchilds($stateParams, _childsService) {
  console.log('getting childs');
  return _childsService.query().$promise;
}

controller

(function () {
  'use strict';

  angular
    .module('childs')
    .controller('childsListController', childsListController);

  childsListController.$inject = ['$stateParams','$state', 'childResolve'];

  function childsListController($stateParams,$state,childResolve) {
    console.log('say hello',childResolve);
    vm.childs = childResolve;
  }
}());
@mleanos mleanos added this to the 0.6.0 milestone Oct 31, 2016
@mleanos mleanos self-assigned this Oct 31, 2016
@mleanos
Copy link
Member

mleanos commented Oct 31, 2016

@o1lab Nothing is jumping out at me..

a) Does your list-childs view load?
b) What's the behavior if you remove the resolve from the childs route?
c) Can you verify the $promise is getting resolved? Try adding a .then() callback on your return _childsService.query().$promise; call -->

return _childsService.query().$promise.then(function (childs) {
  // log something here
  return childs;
});

I don't recommend setting your service calls up this way, from your route resolves. This is just a simple way to test your promise is resolving.

@o1lab
Copy link
Author

o1lab commented Oct 31, 2016

a) No it does not.
b) No change to behaviour - no errors in console.
c) It gets resolved fine and it has the childs data as response.

How do you recommend - get rid of resolves entirely ? Below is what I want to achieve - fairly simple state and route

state --> route
parents --> /api/parents
parents.view --> /api/parents/:parentId
parents.view.childs --> /api/parents/:parentId/childs


Additional information:
Angular is not aware of the childsListController (I believe there is something wrong if there are two resolves to be made in an url of a state) - reason being - if I introduce some injection errors in statement (like below) - I still see no error.

childsListController.$inject = ['$stateParams','$state', 'something_random']

@mleanos
Copy link
Member

mleanos commented Oct 31, 2016

When you removed the resolve from your "childs" route, did you also remove the injection from your controller? If so, and the controller still doesn't load then most likely your angular app is not bootstrapping your controller correctly. What does your childs module configuration look like? Do you have something similar to https://github.com/meanjs/mean/blob/master/modules/articles/client/articles.client.module.js?

I don't think you need to abandon the resolves on your nested routes. It sounds like an issue with the childsListController controller not loading.

I'm on Gitter meanjs right now, if you'd like to take the conversation there.

EDIT: You can verify if your Childs controller is loading by putting a console.log directly in the controller's file.

@shanavas786
Copy link
Contributor

Is there a <ui-view></ui-view> in modules/parents/client/views/view-parent.client.view.html ?

@o1lab
Copy link
Author

o1lab commented Nov 1, 2016

@mleanos : unfortunately I didn't see your message before I closed my day yesterday. I appreciate your offer for help. I've got it working now (see below). Thank you.
@shanavas786 : thank you very much explicitly typing modules/parents/client/views/view-parent.client.view.html :) . It was the problem. I had ui-view tag inside child view and I had believed it was in parent view!

  <div class="ui-view">

  </div>

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants