2

I want to create a controller inside one of my views .. I have a structure like this

index.html
pages > home.html
js > app.js, controllers.js

app.js:

angular.module('app').config(function ($routeProvider) {

    $routeProvider.when('/home', {templateUrl: '/pages/home.html', controller: 'HomeCtrl'});
});

controllers.js :

angular.module('app.controllers', []).controller('HomeCtrl', function(){ ... });

Everything is working great but is it possible to create another controller inside home.html file ?

I tried this but not working

home.html :

<div ng-controller="InsiderCtrl">
</div>

<script type="text/javascript">
    angular.module('app.controllers').controller('InsiderCtrl', function(){ ... });
</script>

But when I tried using function instead it worked! Can anyone explain me why? Isn't it possible to access the modules inside a view ?

<div ng-controller="InsiderCtrl">
</div>

<script type="text/javascript">
    function InsiderCtrl() {}
</script>
3
  • Why would you want to create a controller inside if a view? Commented Jan 31, 2014 at 12:54
  • In laravel framework you can create packages to reuse, I created one that contains the index page and defines the controllers and all js files so I can't modify these because my modifies will be reverted every time I use composer update Commented Jan 31, 2014 at 12:59
  • I am also interested in this question. Commented Sep 28, 2015 at 8:00

0

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.