-1

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..

  • 2
    what's the purpose of doing this? – sdfacre Jan 25 '16 at 04: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 '16 at 04:49
  • @sdfacre beacause my views are inserted dynamically. So i need to execute after it loads. – Vaibhav Chiruguri Jan 25 '16 at 05: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 '16 at 05:31
  • @miensol my controller views are dynamic. i.e., It appends to the body after certain event. Normally controller compiles the static html. Angularjs doesnt complie late or lazy html dom elements. Hope you got the point. – Vaibhav Chiruguri Jan 25 '16 at 05:44
  • Could you have the controller and a basic template then use $compile on the loaded content? https://docs.angularjs.org/api/ng/service/$compile –  Jan 25 '16 at 05:46
  • 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 '16 at 05:47

1 Answers1

0

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.

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41