Monday, 22 June 2015

'this' in angularjs controller

This is your html:

<!DOCTYPE html>
    <script type="text/javascript" src="angular.js"></script>
    <script type="text/javascript" src="demo.js"></script>
  <body ng-app="app" ng-controller="controller">
    <h1>{{1 + 1 + 1}}</h1>

    <input ng-init="numberb = 2" ng-model="numbera" type="number" placeholder="123">
    <input ng-model="numberb" type="number">
    {{numbera + numberb}}

    <button ng-click="vm.aa()">aa</button>
    <button ng-click="">bb</button>
    <button ng-click="xx()">xx</button>

This is your controller:

angular.module('app',[]).controller("controller",function ($scope){
$scope.vm = this;
function aa(){
}; = function(){

$scope.xx = function(){

"this" in controller is the instance of the controller.

function aa belong to the function : "function ($scope){}"

"function ($scope){}" is the constructor of your controller.

So, aa() function will not able to get called in able example.

No comments:

Post a comment