Mou
Mou

Reputation: 16282

AngularJS how to declare private variable and function in controller

i wrote a small controller where i declare a variable with var keyword which is not in scope. does it means that is private in scope ? see my code.

<div ng-app="myApp" ng-controller="myCtrl">
{{test}}
</div>

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
        var test='test hello';
    $scope.Operantion = 'hello';
    $scope.GetData = function () {
        abc();
    };

    function abc()
    {
        alert(test);
    }
    $scope.GetData();
});

the var test variable should be consider as private variable ?

if i declare a function with just function xxx() then it should be consider as private function. i am new in angular. so when testing code then many question is coming to my mind. so please guide me. thanks

Upvotes: 1

Views: 10523

Answers (3)

salman mulla
salman mulla

Reputation: 1

import {Pipe, PipeTransform} from '@angular/core';

@Pipe({ name: 'reverse' })
export class ReversePipe implements PipeTransform {

    transform(arg1) {
       let data = '';
       for (let i = 0; i < arg1.length; i++) {
          data = arg1[i] + data;
       }
       return data;
    }
}

Upvotes: -1

Victor Benetatos
Victor Benetatos

Reputation: 416

Scope is an object, that the view can see and read values from it. By declaring a 'var', you are creating an object outside of the scope, but in the context of the controller function.

BUT the html, that comes with that controller via the ng-controller attribute for example, can only see properties of the scope of that controller.

Upvotes: 2

MarcoS
MarcoS

Reputation: 17711

Yes. var test and function xxx() should be considered "private" or, better, local data and function.

For reference see:
Angularjs scope
What is the scope of variables in javascript

Upvotes: 6

Related Questions