What is controller in AngularJS?
A controller is a set of JavaScript functions which is bound to a specified scope, the ng-controllerdirective. Angular will instantiate the new controller object, and injects the new scope as a dependency. It contains business logic for the view and avoids using controller to manipulate the DOM.
Controller Rules
• We can use controller to set up the initial state of the scope object and add behavior to that object.
• We do not use controller to manipulate DOM. It should contain only business logic and can use data binding and directives for the DOM manipulation.
• We do not use controllers to format input but can use angular from controls instead of that.
• We do not use filter output but can use angular filters instead of that.
• We do not use controllers to share code or state across controllers but can use angular services instead of that.
• We do not manage the life-cycle of other components.
Creating a Controller
• Requires ng-controller directive.
• Add controller code to a module.
• Name your controller based on functionality.
• Controllers are named using camel case (i.e. SimpleController).
• Setup the initial state of the scope object.
ng-Controller directive
ng-Controller directive is an associated controller class to the view.
How to use ng-Controller
1.
2.
3.
4.
Comments
Post a Comment