Reputation:
Hi im new to angular and js and i was learning ng-show. I found that if condition in controller was not able to slice or substring the value
My code was
<tbody>
<tr>
<td>
<input type="text" ng-model="row1" size=6/ disabled>
</td>
<td>
<input type="text" ng-model="dup_row1 " size=6/>
</td>
</tr>
<tr ng-show="row2()">
<td>
<input type="text" ng-model="dup_row1" size=6/>
</td>
<td>
<input type="text" ng-model="dup_row2 " size=6/>
</td>
</tr>
</tbody>
and controller contains
$scope.row2=function(){
if($scope.dup_row1.substr(0,2) > 24){
return true;
}
}
i only wanted to check first two number to ng-show the next row
Any suggestions are appreciated, also fiddle link http://jsfiddle.net/u3yj39u9/
Thanks in advance
Upvotes: 3
Views: 115
Reputation: 171690
Code throws immediate error on initial digest that $scope.dup_row1
is undefined and your function only returns when the intiial condition is met but doesn't return anything if it isn't
A simple fix:
$scope.row2=function(){
if($scope.dup_row1 && $scope.dup_row1.substr(0,2) > 24){
return true;
}
return false;
}
Upvotes: 0
Reputation: 1723
Always follow the "best practice" of always have a '.' in your ng-models, and you avoid a lot of problems.
so do on your controller:
$scope.tableVals = {};
And then all of your values should be properties of this object.
This article might be helpful.
Here you have a working example
Upvotes: 1