Here is my HTML:
<div ng-controller = "myCntrl">
<pre>floor 1: {{Math.floor( value ) }}</pre>
<pre>floor 2: {{value }}</pre>
<pre>floor 3: {{value | number : 0 }}</pre>
<pre>floor 1 from controller: {{newValue }}</pre>
</div>
Controller
app.controller('myCntrl', function ($scope) {
$scope.value = 1233.8435;
$scope.newValue = Math.floor(1233.8435);
});
The output:
floor 1:
floor 2: 1233.8435
floor 3: 1,234
floor 1 from controller: 1233
Generally I'm looking for the proper way to get 1233
.
I don't want to invoke new method in controller.
Why Math.floor
returns nothing?
Thanks,
Demo Fiddle
In this case, AngularJS <span>{{value}}</span>
(expression) is shorthand for <span ng-bind="value"></span>
and ng-bind
acts on the current scope
.
Your current scope is the one defined by myCntrl
so ng-bind="value"
is evaluated as $scope.value
.
AngularJS has no way of making a distinction between the global Math
namespace and $scope.Math
, so it treats it like any other scoped expression.
I'd advise against using $scope.Math=Math;
.
The correct way to do it is using a filter:
angular.module('myApp',[]).filter('floor', function(){
return function(n){
return Math.floor(n);
};
});
Expressions are evaluated on the $scope. If you do this in your controller
$scope.Math=Math;
it would work.
Put it in your controller
<div ng-controller = "myCntrl">
<pre>floor 1: {{Mathvalue}}</pre>
<pre>floor 2: {{value }}</pre>
<pre>floor 3: {{value | number : 0 }}</pre>
</div>
var app = angular.module('myModule', []);
app.controller('myCntrl', function ($scope) {
$scope.value = 1233.8435;
$scope.Mathvalue = Math.floor(1233.8435);
});
app.$inject = ['$scope'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With