How to parseInt in Angular.js

前端 未结 7 1682
生来不讨喜
生来不讨喜 2020-11-28 13:07

Probably, it is the simplest thing but I couldn\'t parse a string to Int in angular..

What I am trying to do:



        
7条回答
  •  忘掉有多难
    2020-11-28 14:08

    Option 1 (via controller):

    angular.controller('numCtrl', function($scope, $window) {
       $scope.num = parseInt(num , 10);
    }
    

    Option 2 (via custom filter):

    app.filter('num', function() {
        return function(input) {
           return parseInt(input, 10);
        }
    });
    
    {{(num1 | num) + (num2 | num)}}
    

    Option 3 (via expression):

    Declare this first in your controller:

    $scope.parseInt = parseInt;
    

    Then:

    {{parseInt(num1)+parseInt(num2)}}
    

    Option 4 (from raina77ow)

    {{(num1-0) + (num2-0)}}
    

提交回复
热议问题