Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How about embedding angularjs controller in a function. So that we can initiate whenever we want.

 fucntion execute() {

 angular.controller('ProductsPageController', ['$scope', '$http',   'InitService', function ($scope, $http, InitService) {
 })

  }

I think this a noob angularjs question. Please let me know the best approach to initiate controller without other modules like ngrouter etc..

share|improve this question
2  
what's the purpose of doing this? – sdfacre Jan 25 at 4:37
1  
you probably want to have a look on requirejs...it will allow you to lazy load files and organize them into modules. And your controller will be within a function as you want)) – Fedor Skrynnikov Jan 25 at 4:49
    
@sdfacre beacause my views are inserted dynamically. So i need to execute after it loads. – Vaibhav Chiruguri Jan 25 at 5:16
    
@VaibhavChiruguri Why do you need to define controllers after the views are loaded? What do you mean exactly by loading views? – miensol Jan 25 at 5:31
1  
are you trying to manipulate the template DOM in that controller? if no, put ng-controller on the root element of the template should be enough. – sdfacre Jan 25 at 5:47

you dont need to add controller inside any function. you can use it onece you initialize it. eg.

angular.controller('ProductsPageController', function  () {
// here you can wright your business logic
});

then initialize it in your app so you can call your controller directly.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.