ng-controller

What is ng-controller:ng-controller is like a class . It is added a scope to current AngularJs module..
Description of ng-controller:ng-controller is like a class .It is added a scope to current AngularJs module.
It is important part of AngularJs 's MVC pattern.
Model- All properties of a controller are model.
View-The Html with AngularJs expression/models .
Controller-A class which added a separate of AngularJs to its current module. And all business logic is present here.

Priority of this directive is 1500..
Syntax of ng-controller:
<element ng-controller="angularjsexampleCtlr">
   
</element>
<element ng-controller="angularjsexampleCtlr as ctlr">


</element>
How to Use ng-controller/ng-controller Example::
Example 1:
<div ng-controller="angularjsexampleCtlr">
    <div>
        Angularjs Example of ng-controller   
        <input type="button" ng-click="myfun()" />
    </div>

</div>
<script>
    angular.module('webapp', []).controller('angularjsexampleCtlr', function ($scope, $http) {
        $scope.myfun = function () {
           alert("Hi this is example of ng-controller.")
        }
    });
</script>
Example 2:

<div ng-controller="angularjsexampleCtlr as Ctlr">
    <div>
        Angularjs Example of ng-controller
        <input type="button" ng-click="Ctlr.myfun()" />
    </div>
</div>
 angularjsexampleCtlr2
<script>
    angular.module('webapp', []).controller('angularjsexampleCtlr', function ($scope, $http) {
        angularjsexampleCtlr.myfun = function () {
            alert("Hi this is example of ng-controller.")
        }

    });

No comments :

Post a Comment