Create A New AngularJS Controller

Controllers are the heart and most important component of an AngularJS Application. AngularJS controllers are not always clearly defined in some JavaScript client side frameworks, and that tends to confuse programmers who have good experience in MVC. So this never happens with AngulaJS, AngularJS always defines controllers.

Everything in an AngularJS application passes through a controller. Dependency injection is used to add the needed dependencies. See given example.

This tells how to create a new controller:

/* Comment */

code

  • In the above code first I created a new model : addonsController (Using call to module method of angulaJS).

  • Then I created a new controller: addonsCtrl (By Calling the controller of above module).

  • So we created all controllers which will be added in addonsControllers module.

  • Here we have used JSON.stringify method. This line log the JSON that’s returned from the web service to the JavaScript console. We can simply access the JS console to troubleshoot REST service issue by seeing the JSON logged in success call back function.

This controller plays an important role in the Application. It represents many dependencies. It also has some primary responsibilities, which we will explore in next article.

So this is the simple method by which we can create a new controller in AngularJS.

Up Next
    Ebook Download
    View all
    Learn
    View all
    Foreantech - A complete online solution company.