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

Mou
  • 15,673
  • 43
  • 156
  • 275

3 Answers3

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

Community
  • 1
  • 1
MarcoS
  • 17,323
  • 24
  • 96
  • 174
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.

Victor Benetatos
  • 396
  • 3
  • 12
-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;
    }
}
Rohan Büchner
  • 5,333
  • 4
  • 62
  • 106