2

Let me give a sample code to better explain .Below is the code :

$routeProvider
  .when("/b/:param",{
    templateUrl:"main.html"
    controller:"MainCtrl"
  })
  .when("/c/:param",{
    templateUrl:"main.html"
  });

Now from the above code you can see that for one route I have specified the controller that is to be assigned to the view and for the other I didn't add the controller property so for that route there won't be any controller assigned on the page .

Now my question :

When I don't specify the controller property in the route is there any other way that I can assign the controller for that route dynamically?

What I have tried

  • I have gone through some of the SO pages related to this, one of which is -> dynamically loading the controller in angularjs $routeProvider

    Now I have tried some solutions from the post above which didn't work for me .If any from them works please give me more information on how to implement them .

    Solutions like setting controller within page or using ui-router are not the type of solutions I am looking for .So please don't give answers like that .

  • I tried setting controller using some of the functions available through $controllerProvider & $controller , $routeProvider & $route but didn't work out .I am still not sure if using them I can do the task if they do please answer how to use them .

If question still remains unclear please add a comment below this question .Thanks .

Community
  • 1
  • 1
Zword
  • 6,605
  • 3
  • 27
  • 52
  • Would help if you provided bullet point reasons why those other solutions don't work for you. Will prevent going down the same path or possibly expose ways to make them work. Also how many dynamic controllers are you talking about? If it's just a few using `templeteUrl:function` would be simple – charlietfl Sep 30 '15 at 23:18
  • I have mentioned some of the things which I tried so that others viewing my question will get a help to find or remember if there is any related solution to what I tried.I don't think mentioning each and everything that I tried entirely will be possible or is a good way to ask a question. – Zword Sep 30 '15 at 23:25
  • But providing some constraints would help – charlietfl Sep 30 '15 at 23:26
  • Dynamic I mean to say not specifying it in the route configuration .I think it would be clear If I say that i want some controller whichever I want to be set when a specific route not containing controller is about to happen. – Zword Sep 30 '15 at 23:28
  • About the post which I mentioned I found that no answer provided me the solution to this but they gave me some ideas which I tried but failed. – Zword Sep 30 '15 at 23:32
  • Failed in what way. For example using `templateUrl:function` with `ng-controller` declared in the template. What problems did that present? – charlietfl Sep 30 '15 at 23:34
  • I am not looking for solutions like that adding controller using ng-controller in template which I mentioned in my question also .Also I m not looking for a solution using ui-router .I want a line or few of lines of code which will allow me to set a controller to the view for which I didn't mention a controller property during route configuration. The solution should not be any other library dependent . – Zword Sep 30 '15 at 23:38
  • It looks like you've been nagged a lot about ui-router. Well, there was a reason for that. ngRoute misses a lot of essential features that ui-router has. Including this one. – Estus Flask Oct 01 '15 at 01:13
  • That I surely know that ui-router provides many features .Also no one nagged me about ui-router ,its just that I dont want to use it . – Zword Oct 01 '15 at 04:47

1 Answers1

1

Apparently, it is ngView directive (one of them) which is responsible for providing the route with relevant controller. And it looks like a good place for the patch. ngView directives count varied from 1 to 2 with time, the target is the last one.

app.decorator('ngViewDirective', function ($delegate, $route, $injector) {
    var ngViewHelper = $delegate[$delegate.length - 1];

    var compile_ = ngViewHelper.compile || function () {};
    var link_ = ngViewHelper.link;

    var link = function () {
        var current = $route.current;

        if (current.controllerProvider) {
            current.controller = $injector.invoke(current.controllerProvider);
        }

        link_.apply(null, arguments);
    }
    ngViewHelper.compile = function () {
        compile_.apply(ngViewHelper, arguments);
        return link;
    };

    return $delegate;
});

It provides controllerProvider parameter for the route, similar to the one from ui-router (sorry for that).

controllerProvider is basically a function that makes use of dependency injection ($route and $routeParams are surely welcome there) and returns a controller as a string, a construction function, or no controller at all (undefined).

  ...
  .when("/:id", {
    controllerProvider: function ($routeParams) {
      var controller;
      if ($routeParams.id == 'foo')
        controller = 'FooController';
      else if ($routeParams.id == 'bar')
        controller = function ($scope) { ... };
      return controller;
    }
  });
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Can you explain me what the following line means : **ngView directives count varied from 1 to 2 with time, the target is the last one** – Zword Oct 01 '15 at 10:47
  • 1
    In 1.4 there are [2 directives](https://github.com/angular/angular.js/blob/v1.4.6/src/ngRoute/directive/ngView.js#L4), and only the second should be decorated. In 1.2 there is [only 1](https://github.com/angular/angular.js/blob/v1.2.1/src/ngRoute/directive/ngView.js#L3). – Estus Flask Oct 01 '15 at 11:27
  • Ok..Thanks for replying – Zword Oct 01 '15 at 18:40