Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function from global API in Angular expressions?

Tags:

angularjs

Angular.js provides a banch of global functions like angular.lowercase, angular.isArray etc.

Suppose I have a string in my scope:

function MyController($scope) {
   $scope.myString = "TEST"
}

Calling angular.lowercase has no effect:

{{lowercase(myString)}}
{{angular.lowercase(myString)}}

How can I call such function in my template?

UPDATE

Example with angular.isArray

<div ng-show="isArray(myVar)">...</div>
like image 206
ponomandr Avatar asked Apr 05 '13 22:04

ponomandr


People also ask

Which of the following global variable Cannot be directly accessed by angular expression?

AngularJS expressions do not have direct access to global variables like window , document or location .

What does :: mean in angular?

:: is used for one-time binding.


1 Answers

The expression in the {{}} is not actual javascript although it looks that way - it's an angularjs expression. For this reason not everything will be available to you.

charlietfl is right that your particular case can be solved with an existing filter. Not every angular.* function is exposed this way, however, in which case you should create your own custom filters.

Filters are cleanest but as a dirty workaround you could also just have the following line in your controller:

$scope.lowercase = angular.lowercase;  // not angular.lowercase()
like image 71
Roy Truelove Avatar answered Nov 02 '22 07:11

Roy Truelove