1

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

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

how to declare private variable and function inside controller ?
just use 'var aa'. it will be local to your controller
-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;
    }
}

1 Comment

That is not AngularJS

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.