The ng-controller directive is used to attaches a controller class to the view (HTML element).
The ng-controller directive creates a new scope.
The ng-controller directive compile at the priority level 500.
It is supported by all HTML element.
The ng-controller is a key aspect of how angular supports the Model-View-Controller design pattern.
MVC Component in AngularJS:
Model – The models are the properties of a scope. This is the lowest level of the pattern. It is used to manipulating the data. The scope properties are accessed through the bindings.
View – The view is a template (HTML with data bindings). It is used to displaying all or part of the data to the user.
Controller – The ng-controller directive is used to define the controller classes. The controller class contain a software code that controls the interaction between the Model and View (HTML).
Syntax for ng-controller directive in AngularJS:
Parameter value for ng-controller directive in AngularJS:
Sample coding for ng-controller directive in AngularJS:
Data:
The msg data been defined for our AngularJS Application.
Logic:
Controller logic for the AngularJS application
HTML:
Viewable HTML contents in AngularJS Application.
Code Explanation for ng-controller Directive in AngularJS:
AngularJS is distributed as a JavaScript file, and can be added to a HTML page with a <script> tag.
The AngularJS application is defined by ng-app="myApp". The application runs inside the <div> tag. It’s also used to define a <div> tag as a root element.
The ng-controller=”myController” is an AngularJS directive. It is used to define a controller name as “myController”.
{{ msg }} is a used to bind the controller value by using the scope object.
angular.module function is used to create a module. Here has passed an empty array to it.
Here we have declared a controller myController module using apps.controller() function.
The value of the controller modules is stored in scope object. In AngularJS, $scope is passed as first argument to apps.controller during its constructor definition.
Here we have set the value of $scope.msg as “Welcome To AngularJS Tutorials” which are to be used to display the {{ msg }} value in the HTML<div> element.
Sample Output for ng-controller Directive in AngularJS :
The output displays a message of a variable msg by using a ng-controller directive.
Related Searches to angularjs ng-controller directive
ng-controller not workingangularjs controller parameterswhat is link function in angularjsfactory method in angularjswhat are directives in angularjsdoes angular work with mobile browsersng-controller ng-includeservices in angularjsng-controller exampleangular directive scopeangular directive controllerangular directive link vs controllerangularjs tutorials