1

I want to evaluate some js predefined function in angular expression. For example new Date(). But, not able to do that. If it is possible, please explain how to do that.

For example:

<input type="text" ng-init="yearFromSelector=new Date().getFullYear()"  ng-model="yearFromSelector">

or

{{new Date().getFullYear()}}

I know that we can do that in controller and send value to view using $scope. But, I want to know is it possible to do it in angular expressions itself.

Thanks in advance.

1
  • Please show, the code of your controller. Commented Jan 6, 2017 at 12:39

1 Answer 1

2

You can't construct instances in Angular expressions. You will need to use some controller code:

ng-init="yearFromSelector = getDate().getFullYear()"

where you would need to define getDate helper function either in controller or globally in run block (and attach it to $rootScope):

$scope.getDate = function() {
  return new Date
}

Read about what Angular expression allow and what do not: https://docs.angularjs.org/guide/expression

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

1 Comment

Yeah, I have already tried the same using controller. But, I want to know how to do it in expression itself if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.