Angularjs expression does not work for ng-bind and {{}}

Adding Angularjs does not work for ng-bind and {{}}, but multiplication works, why? I have the following code shown below

single_style.thread = 3;
single_style.stiching = 5;

and

1) <td>{{single_style.thread + single_style.stiching}} </td>
2) <td>{{single_style.thread * single_style.stiching}}</td>

1) First I get the answer as 35

2) second I get the answer as 15

Even I use ng-bind, also doesn’t work why?

+4
source share
2 answers

Update:

The problem was as suggested, you tried to add lines that led to concatenation after further reading and this big SO post shown below.

SO Answer

which says

Angular does not use JavaScript eval () to evaluate expressions. Instead, Angular $ parse services process these expressions.

Angular , , . . - .

, parseInt Angular, javascript, Angular. , (*) , 1, , number. , . , , . Html .

<td>{{single_style.thread*1 + single_style.stiching*1}} </td>

Plunkr Demo

, . + , , , , 3 5 35, , , multiply (*) type conversion , (15), !

<td>{{parseInt(single_style.thread) + parseInt(single_style.stiching)}} </td>
<td>{{parseInt(single_style.thread) * parseInt(single_style.stiching)}}</td>
+2

, - .

+ '3' '5', * int . - /

[EDIT]

:

$scope.all_value = [];
    angular.forEach(data, function(item){       
        item.thread = parseFloat(item.thread);
        item.measure = parseFloat(item.measure);
        item.bulk_qty = parseFloat(item.bulk_qty);
        item.price = parseFloat(item.price);        
        item.pack_charge = parseFloat(item.pack_charge);
        item.label = parseFloat(item.label);
        item.elastic = parseFloat(item.elastic);
        item.button = parseFloat(item.button);         
        item.markt_price = parseFloat(item.markt_price);
        item.stiching = parseFloat(item.stiching);           

        $scope.all_value.push(item);
    })

$scope.single_style ={
  thread: 3,
  stiching: 5
};

<p>{{single_style.thread + single_style.stiching}} </p>
<p>{{single_style.thread * single_style.stiching}}</p> 

8    
15

, , :

$scope.single_style ={
  thread: '3',
  stiching: '5'
};

<p>{{single_style.thread + single_style.stiching}} </p>
<p>{{single_style.thread * single_style.stiching}}</p> 

35    
15

int , a.e.:

$scope.single_style ={
  thread: parseInt('3'),
  stiching: parseInt('5')
};

+2

Source: https://habr.com/ru/post/1685352/


All Articles