3

I'm trying to figure out how resolve http ajax get calls with multi-view using UI-Router lib for AngularJs.

JS (app.js):

angular
    .module("goHenry", ["ui.router"])
    .controller("MainCTRL", MainCTRL)
    .config(configB);



function MainCTRL($location){
    this.nameApp = "goHenry";

}

JS (router.js):

function configB($urlRouterProvider, $stateProvider){        
    $urlRouterProvider.otherwise("/");    
    $stateProvider
        .state("/",{
           url: "/",
           templateUrl : "/testingBlock.htm",
           controllerAs : "MainCTRL as ctrl"
        })
        .state("multi",{
            url: "/multi",
            views: {
                "": {
                    templateUrl: "/multipleView.htm",
                    controllerAs: "MainCTRL as ctrl"
                },
                //blocks
                "viewA@multi": {
                    resolve: {
                        getChildrenNumber: function($http){
                            //below here I'm simulating some GET answer
                           return "Some response from an API";
                        }    
                    },
                    templateUrl: "/testingBlock.htm",
                    controllerAs: "MainCTRL as ctrl"
                },
                "viewB@multi": {
                    templateUrl: "/app/templates/login.htm",
                    controller: function($scope){
                        $scope.nameApp = "nameAppChanged";
                        //$scope.getChildrenNumber = getChildrenNumber;
                    }    
                }
            }
        });        
    }

Should/Can I resolve the request inside the main view or inside the sub-view? Then, how can I use that result in a sub-view and/or in the main/view, I mean in their own controller.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Donovant
  • 3,091
  • 8
  • 40
  • 68

1 Answers1

3

There is a working plunker

Let's start with controllers, for our ViewA and ViewB:

.controller('MainCTRL', ['$scope', 'getChildrenNumber', 
  function($scope, getChildrenNumber) {
    $scope.children = getChildrenNumber;console.log($scope.children)
  }])
.controller('ViewBCtrl', ['$scope', 'getChildrenNumber', 
  function($scope, getChildrenNumber) {
    $scope.children = getChildrenNumber;
}])

And they will be properly provided with 'getChildrenNumber', if we define state like this:

.state("multi", {
      url: "/multi",
      views: {
        "": {
          templateUrl: "multipleView.htm",
          controllerAs: "MainCTRL as ctrl"
        },
        //blocks
        "viewA@multi": {
          templateUrl: "testingBlock.htm",
          controller: "MainCTRL",
          controllerAs: "ctrl",
        },
        "viewB@multi": {
          templateUrl: "app/templates/login.htm",
          controller: "ViewBCtrl",
          controllerAs: "ctrl",
        }
      },
      resolve: {
        getChildrenNumber: ['$http', function($http) {
          return $http
              .get("data.json")
              .then(function(response){
                  console.log(response.data)
                  return response.data;
              })
        }]
      },
});

As we can see - resolve was moved from the 1) view level defintion into 2) state level definition. That means, that we can ask for such resolved values in any of the views' controller

Also small note, with UI-Router we should use this controllerAs notation:

"viewA@multi": {
  templateUrl: "testingBlock.htm",
  controller: "MainCTRL", // controller name
  controllerAs: "ctrl",   // the AS part - what will be injected into $scope

Check it in action here

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • I can't make it to work for controller defined into app.js file, since that I'm defining routes in a different file (routes.js) The error I got is "Unknown provider: getChildrenNumberProvider <- getChildrenNumber <- MainCTRL" – Donovant Jun 10 '15 at 08:48
  • That error is not related to the "different file". That does not matter. But usually, you can get that error, if you use `ng-controller="MainCTRL as ctrl"`. Because this is **not** using the UI-Router stuff... It is dependent on angular IoC - without state resolved locals... If you can make my plunker broken, I am ready to assist to fix it... – Radim Köhler Jun 10 '15 at 08:50
  • This updated version is working now: http://plnkr.co/edit/NdkkCo4mGWAoZCsNwnKx?p=preview. Here it is even splitted into 2 files http://plnkr.co/edit/cplPfmZ78T1PDDDcXFYZ?p=preview (check the someState.js) – Radim Köhler Jun 10 '15 at 09:16
  • 1
    Maybe I am missing something.. but I can see it working http://plnkr.co/edit/kPyrtVlVRPCewNoyIQBG?p=preview (in this version I really consume data.json)... sorry. I do not see the problem. If you do, please, describe what is wrong... I am ready to help if I could ; – Radim Köhler Jun 10 '15 at 09:27
  • Is necessary using ".run" after defined controllers? – Donovant Jun 10 '15 at 10:06
  • 1
    Neccessary? not.. run() is upon us. If we want to do some stuff in run phase we can... The point is, that in run we can access more stuff.. Maybe check this http://stackoverflow.com/q/30477922/1679310.. hope it helped – Radim Köhler Jun 10 '15 at 10:09
  • Sorry, have to stop here. It starts to be over-exceeding the expected scale of one single answer... In case you need another answers, you really should issue new question. You will get more attention then just me, and increase the probability to get what you want... Hope this makes sense to you as well. Good luck with UI-Router – Radim Köhler Jun 10 '15 at 10:46
  • Yes but my question is still the same... It doesn't work as I wanted, because you wrote the code in different way. – Donovant Jun 10 '15 at 10:51
  • OK, my last try... http://plnkr.co/edit/ctfLnNA4eix8gCpXnk43?p=preview or more correct http://plnkr.co/edit/SlM9uBnZsCOVGLxiRmdu?p=preview The point is, I am using annotation which would later save your life... once you will start to use minification... without my approach, your code won't work. That's why I force usage of that in the index.html with `` ... search for NgStrictDi – Radim Köhler Jun 10 '15 at 10:55
  • Ok, thanks, I'm trying to understand what doesn't work on my code, that actually it's the same of yours now. – Donovant Jun 10 '15 at 11:28