Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Call function in HTML

I have made a function getAge() in my custom.js. This function needs to be called in my HTML page.

So what i tried is like this :-

<td>{{getAge(user.basicinformation[0].dateofbirth)}}</td>

I can see no results.

How to call function in HTML?

like image 461
Anup Avatar asked May 22 '14 05:05

Anup


People also ask

How do you call a function in AngularJS?

You can call your angularJS function from javascript or jquery with the help of angular. element().

How to call AngularJS function on page load?

Case 1 : Using angularJs alone: To execute a method on page load, you can use ng-init in the view and declare init method in controller, having said that use of heavier function is not recommended, as per the angular Docs on ng-init: This directive can be abused to add unnecessary amounts of logic into your templates.

What is AngularJS in HTML?

AngularJS is a JavaScript framework. It can be added to an HTML page with a <script> tag. AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.


2 Answers

So create a filter for getAge.

app.filter("getAge", function(){
   return function(input){
      // Your logic
      return output; 
   }
});

Then call it in HTML:

<td>{{ user.basicinformation[0].dateofbirth | getAge }}</td>
like image 141
Nitish Kumar Avatar answered Sep 27 '22 05:09

Nitish Kumar


Attach getAge and user to the $scope of the pages controller.

$scope.user = user;
$scope.getAge = getAge;

Make sure you're doing this in the controller and setting up the controller correctly. It won't work unless you've set up a controller with this DOM view and injected the $scope service into the controller.

like image 41
BlinkyTop Avatar answered Sep 28 '22 05:09

BlinkyTop