2

Is it possible to load a custom controller specifying the filename each partial in angularjs?

Something like this:

 var demo = angular.module('demo', ['ngRoute', 'ui.bootstrap', 'demo.filters', 'demo.services', 'demo.directives', 'demo.controllers'])

    .config(['$routeProvider', function ($routeProvider) {


        $routeProvider.when('/custom', 
                   {
                      templateUrl: 'partials/custom.html', 
                      controller:   customController.js
                    });

    });

4 Answers 4

3
+25

With the way require js works you need to set your app up like this:

var demo = angular.module('demo', ['ngRoute', 'demo.filters', 'demo.services', 'demo.directives']);

demo.config(['$routeProvider',
  function($routeProvider) {
    var _routes = ['/custom', '/custom2'];
    _routes.map(function(route) {
      var _cntrlName = route.slice(1, route.length);
      require([_cntrlName + "Controller"], function(controller) {
        $routeProvider.when(route, {
          templateUrl: '.' + route + '.html',
          controller: controller
        });
      });
    });
  }
]);

And your controller code must look like this:

define(function(require, exports, module) {
  function controller($scope) {
    $scope.testvar = "test 1!";
  }
  module.exports = controller;
});

Here is a plunker to demonstrate this http://plnkr.co/edit/N5fE5rezf6eM8jDIz23w?p=info

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

1 Comment

I tried your solution but unfortunatly it still not working plnkr.co/edit/WZaJDbSoAhf22G3iC5DL?p=preview
2

Angular is not able to load JS files, but you can do this with RequireJS

var demo = angular.module('demo', ['ngRoute', 'ui.bootstrap', 'demo.filters', 'demo.services',     'demo.directives'])

demo.config(['$routeProvider', function ($routeProvider) {
    var _routes = ['/custom', '/somepage']

    _routes.map(function(route){
        var _cntrlName = route.slice(1, route.length);
        $routeProvider.when(route,{
             templateUrl: 'partials' + route;
             controller: require('./' + _cntrlName + 'Controller.js');
        })     
    })  
});

Controller file:

(function(){
   function customController($scope){
       console.log($scope);   
   };
   return 'customController';  
)()

5 Comments

This is exactly what I need, but probably there are some mistakes in the code. Could you kindly provide to create a working fiddle? Thank you so much
Fixed the code, but i can't reproduce this on fiddle, there is no file system.
..it doesn't work yet, and the error is the same on my main project. plnkr.co/edit/RfRFPq2OVNr5I6iGVtdq?p=preview
Later today i do it localy and give you link to repository.
you should try using plunker
0

Instead of specifying a file put the controller's name. However, you should remove ng-controller directive from your partial.

Comments

0

Yes. you can load the other controller scope inside a different controller.

Sample Code:
var mode = angular.module().config(function(){

controller("controllerName",{$scope : scope}); // This line is important

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.